[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Need help with regexp

Need help with regexp

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Gunner   Argentina  (30 posts)  [Biography] 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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,928 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (22,928 posts)  [Biography] 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
[Go to top] top

Posted by Gunner   Argentina  (30 posts)  [Biography] 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
[Go to top] 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.


8,967 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]