Help with my string pattern

I’m trying to replicate the emoji stuff in Roblox Devforum and/or Discord

Like when you type :sa it will show you this
image

I am trying to do that with this string pattern

^.*%s:([^%s%p]+)$

But the problem is if it is at the beginning of the string it will return nil

How could I check for %s only if there is something before the colon? like at this part “^.*%s:”

For example this is what I want it to do

:test returns “test” (nothing before the colon so we don’t check for spaces)

hi:test returns nil (something before the colon, but no space)

hi :test returns “test” (something before the colon and space)

I want to do this with just a single string pattern No if statements pls Thanks

3 Likes

I researched your issue

  • ^.*%s: matches any string that starts with any characters (including none), followed by a space and a colon.

Here is a pattern that should work

^(.-)%s?:([^%s%p]+)$

A quick breakdown:

  • :test returns test (nothing before the colon, so it doesn’t check for spaces).

  • hi:test returns nil (something before the colon, but no space).

  • hi :test returns test (something before the colon and space).

1 Like

image

1 Like

Try printing it like this

print("test":match("^(.*)$"))
print("hi test":match("^(.*)$"))
print("Hi test":match("^(.*)$"))

The output should be this:

test
hi test
Hi test
1 Like

Bro you are using ChatGPT 100%

5 Likes

researching it for you on google to help you??

1 Like

I did bro Why do you think I’m asking on here now

No I said im researching it for you. I didn’t say it in a bad way.

baki-slap-baki

Lol what did I do wrong I came out of my way to try and research something for you :skull:

Bro please somebody besides this guy help me

Wait, I just realized your name says ChatGPT LooL.

I just don’t use string patterns ¯\_(ツ)_/¯

Didn’t manage to make it a single pattern, but this works.

local pattern = "%f[%S]:%w+" 
local pattern2 = "^:%w+"
local str = ':one :two:three' -- Expect :one and :two

for cmd in string.gmatch(str,pattern) do 
  print(cmd) 
end 

print(string.match(str,pattern2))

-- :dd 
-- :Dd

1 Like

This isn’t possible with Lua string patterns.

Is there a reason why it needs to be one pattern and with no if statements? The only way I would see how to do it without either of those is to always prepend the string you’re searching with a space, i.e.

text:gsub("^([^%s])", function(s) return " " .. s):match("^.*%s:([%w_]+)$")

which still technically breaks your “one pattern” rule (and the first pattern is essentially “if you don’t start with whitespace, prepend with a space”).

Edit: added the end of input magic character to the pattern

1 Like

Bro he was using ChatGPT to help me

that is not how you respond to a potential ChatGippity user
still hilarious

1 Like

I have converted your REGEX into a way to understand what it’s doing, for others to read.

^.*%s:([^%s%p]+)$

Symbol Sentence
^ Locate at the start of a string…
.* a set of random characters (can be none)…
%s with a white space…
: followed by a semicolon…
([^%s%p]+) with any set of characters that doesn’t contain any white space or punctuation marks…
$ which must take up the whole string.

Where it’s ending at is the %s, because if your string only contains :test, it will fail because there is no white space characters anywhere in the text.

This is impossible to fix in REGEX form, so instead you can prepend your string with a white space at the beginning.

local str = ":abcd"
str = " " .. str

print(str:match("^.*%s:([^%s%p]+)$"))
2 Likes

Why %w_ but thanks I’ll try this later

Oh I understand why u used %w_ now also it works thanks