Reputation: 137
a = "stackoverflow.com/questions/ask"
print(string.match(a,"(.*/)")) -- stackoverflow.com/questions/
print(string.match(a,"(.*/).*")) -- stackoverflow.com/questions/
I can't understand the second result. In my option it should be "stackoverflow.com/questions/ask
" as "(.*/)
" matches "stackoverflow.com/questions/
" and ".*
" matches "ask
". Can someone tell me WHY the second result is "stackoverflow.com/questions/
" ? Does x = string.match(a,"(.*/).*")
and x = string.match(a,"(.*/)")
are same?
Upvotes: 2
Views: 151
Reputation: 462
the () means you have used Captures.so maybe you can use it like this:
print(string.match(a,"((.*/).*)"))
A pattern can contain sub-patterns enclosed in parentheses; they describe captures. When a match succeeds, the substrings of the subject string that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number 1); the character matching "." is captured with number 2, and the part matching "%s*" has number 3.
Upvotes: 1