Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ General
➜ Need help with regexp
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Gunner
Argentina (31 posts) Bio
|
Date
| Tue 30 Mar 2021 03:51 AM (UTC) Amended on Tue 30 Mar 2021 04:48 AM (UTC) by Nick Gammon
|
Message
| Hello.
I am trying to validate text input from an input box.
What I need is for the character string to match a word (not numbers) or two words separated by a space
the function is:
function comprobar_hechizo (dhech)
local match = string.match (dhech, "^%%a+%%s%%a+$")
if not n then
utils.msgbox ("the spell name can only contain letters and spaces", "Error")
return false
end
return true
end
This only works if the string contains two words and a space, but I also want it to work if the string contains only one word.
I have tried this:
local match = string.match (dhech, "^%%a+|%%a+%%s%%a+$")
But don't work. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #1 on Tue 30 Mar 2021 04:35 AM (UTC) |
Message
| What's with the two % symbols?
Anyway, Lua patterns don't have optional things (however the PCRE regexps, used in triggers, do).
You could do this:
test = "foo bar"
if string.match (test, "^[%a%s]+$") then
print "ok"
else
print "fail"
end
That will test for letters and spaces, so it would match one word, two words, three or more words, or even a single space.
A better thing would be to have two tests (one word, or two words) like this:
test = "foo bar"
if string.match (test, "^%a+$") or string.match (test, "^%a+%s+%a+$") then
print "ok"
else
print "fail"
end
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #2 on Tue 30 Mar 2021 04:40 AM (UTC) Amended on Tue 30 Mar 2021 04:41 AM (UTC) by Nick Gammon
|
Message
| Although this might work:
test = "foo bar"
if string.match (test, "^%a+%s*%a*$") then
print "ok"
else
print "fail"
end
That is testing for:
- One or more letters (so you get at least one word)
- Zero or more spaces (so the space is optional)
- Zero or more extra letters (to allow for the second word)
See: http://www.gammon.com.au/scripts/doc.php?lua=string.find |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Gunner
Argentina (31 posts) Bio
|
Date
| Reply #3 on Tue 30 Mar 2021 04:52 AM (UTC) |
Message
|
Nick Gammon said:
A better thing would be to have two tests (one word, or two words) like this:
test = "foo bar"
if string.match (test, "^%a+$") or string.match (test, "^%a+%s+%a+$") then
print "ok"
else
print "fail"
end
Perfect, that has worked for me.
As for the double%, that function is inside a send in an alias | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
11,902 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top