If I have this string: "wwww:xxxx:yyyy:zzzzzz:0"
How can I return only the "zzzzzz:0" value? Knowing that zzzzz will not always be the same length.
CodePudding user response:
Use Patterns for this:
string.match("wwww:xxxx:yyyy:zzzzzz:0", "[^:] :[^:] $") --> 'zzzzzz:0'
This is naïve but will work in the simplest use cases. [^:]
matches a sequence of one or more characters that are not :
, :
after that matches itself, and $
matches the end of the string.
Based on the format of your string, you might actually want to look into separating the string into a table using :
as separator. This can be accomplished with e.g., string.gmatch
and the [^:]
pattern from above.
In a complex situation where the components are expected to e.g., support quoting or escape sequences you might want to use a full-pledged csv-like parser instead.
CodePudding user response:
Try also this somewhat simpler pattern:
string.match("wwww:xxxx:yyyy:zzzzzz:0", ". :(. :. )$")
It looks for the last item of the form foo:bar
.