Reputation: 129
Again I need help with Lua patterns. Lets say i have following strings:
<c> block
- should match c
and block
<category>material
- should match category
and material
decorative
- if there is no <>
block then it should return what it was given (or nil).
I'm not interested in spaces, so built in trim would be nice.
I managed to create (<%a>)([^>]+)
, but it returns <c>
and block
in first example, whish sadly is not what i'm after.
Upvotes: 2
Views: 219
Reputation: 626896
The (<%a>)
part in your pattern captures the angle brackets thus they land in the captured value. The %a
matches only one alpha char, so you need to add +
quantifier after it to match 1 or more alpha chars.
Use
<(%a+)>%s*([^>]+)
The %s*
will match 0+ whitespace chars and they will be outside Group 2.
A Lua demo:
local function splitString(text)
return string.match(text, "<(%a+)>%s*([^>]+)")
end
print(splitString("<c> block")) -- c block
print(splitString("<category>material")) -- category material
print(splitString("decorative")) -- nil
Upvotes: 5