Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Lua Matching vs Regex
Started by jack95 Jun 30 2013 02:45 AM

- - - - -
2 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

jack95

[GLOBAL: userInfoPane.html]
jack95
  • Contributor

  • 198 posts
  • Corona SDK

I want to match an equivalent of the regex pattern:

 

\{(([^\{\}]+)|(?R))*\}

 

which basically matches first { ... } pair+content in a string. I cannot find a complete reference for what the lua pattern matching supports (everything either refers to compiled modules/extensions or alternate libs from string.match). I'm hoping someone here can help me, at least, with the Lua equivalent to R!



[TOPIC: post.html]
#2

dmccuskey

[GLOBAL: userInfoPane.html]
dmccuskey
  • Contributor

  • 156 posts
  • Corona SDK

is this the type of reference you're searching ?

http://www.lua.org/manual/5.1/manual.html#5.4.1
http://www.lua.org/pil/20.2.html

can you give some examples of what you're trying to match ? that regex seems like more than one would need just to get content between two {}s.

here are some Lua examples:
local str, pat

-- easy data
str = "{Hello} in between {World} 1234 {123456}"
pat = "{(%w+)}"
for word in string.gmatch(str, pat) do print(word) end

print("------")

-- complex data
str = "{'Hell0'} in between {W0rld} 1234 {1234} {'.4.4.4.'}"
pat = "{([%w'.]+)}"
for word in string.gmatch(str, pat) do print(word) end

-- output

2013-06-30 15:25:33.418 Corona Simulator[25712:f03] Hello
2013-06-30 15:25:33.418 Corona Simulator[25712:f03] World
2013-06-30 15:25:33.419 Corona Simulator[25712:f03] 123456
2013-06-30 15:25:33.419 Corona Simulator[25712:f03] ------
2013-06-30 15:25:33.419 Corona Simulator[25712:f03] 'Hell0'
2013-06-30 15:25:33.419 Corona Simulator[25712:f03] W0rld
2013-06-30 15:25:33.419 Corona Simulator[25712:f03] 1234
2013-06-30 15:25:33.420 Corona Simulator[25712:f03] '.4.4.4.'

cheers,
dmc

[TOPIC: post.html]
#3

jack95

[GLOBAL: userInfoPane.html]
jack95
  • Contributor

  • 198 posts
  • Corona SDK

Thanks for the links.

 

I was trying to guard against nested curlies to match first outermost pairing. This would allow for iteration through a string with irregular separators between json encoded strings.

 

I have solved the problem a different way, but this has highlighted the Lua matching seems primitive. Corona has to live in a modern-day ecosystem where protocols and formats are rather complex and more powerful pattern matching is needed.




[topic_controls]
[/topic_controls]