Pattern matching of zero or one word

Using the Lua pattern matching (https://docs.coronalabs.com/guide/data/luaString/index.html), how would you write a pattern that matches zero or one word (i.e. not character)?

It should match this:

“no problems”

“problems”

but NOT this:

“not my problems”

I am quite adept at string manipulation, but I simply don’t understand your question.

how would you write a pattern that matches zero or one word … but NOT this:

you don’t (and docs are explicit on that point) but you can easily count words (with gmatch) then test that

Sorry, I was unclear. Look at these examples:

  1. string.find(“problems”, “^problems$”) -> MATCHES

  2. string.find(“no problems”, “^%w+%s+problems$”) -> MATCHES

  3. string.find(“not my problems”, “^%w+%s+%w+%s+problems$”) -> MATCHES

What I need is a pattern that matches both 1 and 2 but not 3.

already answered, go read the docs.  there is no syntax for optional words, it’s all character-based.

ie, cannot craft a pattern to:  require one ‘word’, optionally accept two ‘words’, forbid three ‘words’

otoh, can easily craft a pattern to: require one character, optionally accept two characters, forbid three characters

[edit] unless you’re asking about these three specific literal strings, then a single pattern could work, but not “generically”

Do the reverse. Look for 3 instances of %s separated by 1+ other characters and if you find a match, consider it a fail.

@richard11: in sentences where there are more than two words before “problem” that wouldn’t work. I need to be able to catch any number of words and only give “true” if there are zero or one words before “problem”.

@davebollinger: I read the Lua docs on patterns that you linked to. Counting words would indeed work but do not understand how to achieve that. How would a pattern look that would make gmatch count words?

You can do something as simple as:
 

local s = "there are no problems" local word = {} for substring in s:gmatch("%S+") do table.insert( word, substring ) end print( table.concat( word, ", "))

This will split the string, s, into separate words. You can then do what you want with them.

there are numerous ways you could write it, for instance:

local function test(s) local words = {} for word in s:gmatch("%w+") do words[#words+1] = word end return (words[#words]=="problems" and #words\<=2) end print(test("problems")) print(test("no problems")) print(test("not my problems"))

[EDIT]  xedur simulposted same solution

Ok, that is perfect, thanks! 

I am quite adept at string manipulation, but I simply don’t understand your question.

how would you write a pattern that matches zero or one word … but NOT this:

you don’t (and docs are explicit on that point) but you can easily count words (with gmatch) then test that

Sorry, I was unclear. Look at these examples:

  1. string.find(“problems”, “^problems$”) -> MATCHES

  2. string.find(“no problems”, “^%w+%s+problems$”) -> MATCHES

  3. string.find(“not my problems”, “^%w+%s+%w+%s+problems$”) -> MATCHES

What I need is a pattern that matches both 1 and 2 but not 3.

already answered, go read the docs.  there is no syntax for optional words, it’s all character-based.

ie, cannot craft a pattern to:  require one ‘word’, optionally accept two ‘words’, forbid three ‘words’

otoh, can easily craft a pattern to: require one character, optionally accept two characters, forbid three characters

[edit] unless you’re asking about these three specific literal strings, then a single pattern could work, but not “generically”

Do the reverse. Look for 3 instances of %s separated by 1+ other characters and if you find a match, consider it a fail.

@richard11: in sentences where there are more than two words before “problem” that wouldn’t work. I need to be able to catch any number of words and only give “true” if there are zero or one words before “problem”.

@davebollinger: I read the Lua docs on patterns that you linked to. Counting words would indeed work but do not understand how to achieve that. How would a pattern look that would make gmatch count words?

You can do something as simple as:
 

local s = "there are no problems" local word = {} for substring in s:gmatch("%S+") do table.insert( word, substring ) end print( table.concat( word, ", "))

This will split the string, s, into separate words. You can then do what you want with them.

there are numerous ways you could write it, for instance:

local function test(s) local words = {} for word in s:gmatch("%w+") do words[#words+1] = word end return (words[#words]=="problems" and #words\<=2) end print(test("problems")) print(test("no problems")) print(test("not my problems"))

[EDIT]  xedur simulposted same solution

Ok, that is perfect, thanks!