Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Pattern matching of zero or one word
Started by Divergent Monkey Jun 11 2019 08:46 AM

9 replies to this topic
patterns
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Divergent Monkey

[GLOBAL: userInfoPane.html]
Divergent Monkey
  • Contributor

  • 331 posts
  • Corona SDK

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"


[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 838 posts
  • Corona SDK

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



[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,348 posts
  • Enterprise

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



[TOPIC: post.html]
#4

Divergent Monkey

[GLOBAL: userInfoPane.html]
Divergent Monkey
  • Contributor

  • 331 posts
  • Corona SDK

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.



[TOPIC: post.html]
#5

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,348 posts
  • Enterprise

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"



[TOPIC: post.html]
#6

richard11

[GLOBAL: userInfoPane.html]
richard11
  • Contributor

  • 448 posts
  • Corona SDK

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

[TOPIC: post.html]
#7

Divergent Monkey

[GLOBAL: userInfoPane.html]
Divergent Monkey
  • Contributor

  • 331 posts
  • Corona SDK

@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?



[TOPIC: post.html]
#8

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 838 posts
  • Corona SDK

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.



[TOPIC: post.html]
#9

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,348 posts
  • Enterprise

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



[TOPIC: post.html]
#10

Divergent Monkey

[GLOBAL: userInfoPane.html]
Divergent Monkey
  • Contributor

  • 331 posts
  • Corona SDK

Ok, that is perfect, thanks! 




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: patterns