Regex to match 2 different parts of a line
Question
I'm working in lua, and i need to match 2 parts of a line that's taken in through file IO. I'm inexperienced with regexes and i'm told lua doesn't have full regex support built in (but i have a library that provides that if need be). Can someone help me with building regexes to match the parts necessary?
"bor_adaptor_00.odf" 3.778
^^^^^^^^^^^^^^ ^^^^^
i need this in and this in
a string a number
Solution
^"(.*?)"\s+(\d[\d.]*)$
Explanation:
- ^ = line start
- "(.*?)" = save everything between " and " to a capture group
- \s+ = any number >= 1 of whitespace chars
- (\d[\d.]*) = a digit followed by more digits or dots
- $ = end of line
No idea how to use that in lua, but should help to get you started.
On the other hand, this is a really simple string, so it could be a good idea to parse it without regular expressions.
OTHER TIPS
I made an example:
s = '"bor_adaptor_00.odf" 3.778'
val1, val2 = string.match(s,'(%b"")%s*([.0-9]*)')
print(val1, val2)
output:
"bor_adaptor_00.odf" 3.778
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow