I want to run two different lua string find on the same string " (55)"
Pattern 1 "[^%w_](%d+)"
, should match any number
Pattern 2 "[%(|%)|%%|%+|%=|%-|%{%|%}|%,|%:|%*|%^]"
, should match any of these ( ) % + = - { } , : * ^
characters.
Both of these patterns return 2, why? Also if I run a string match, they return (
and 55
respectivly (as expected).
It seems you are using the patterns with
string.find
that finds the first occurrence of the pattern in the string passed. If an instance of the pattern is found a pair of values representing the start and end of the string is returned. If the pattern cannot be foundnil
is returned.Both patterns find a match at Position 2:
[^%w_](%d+)
finds(
because it is matched with[^%w_]
(a char other than letter, digit or_
), and[%(|%)|%%|%+|%=|%-|%{%|%}|%,|%:|%*|%^]
matches the(
because it is part of the character set.However, the first pattern can be re-written using a frontier pattern,
%f[%w_]%d+
, that will match 1+ digits if not preceded with letters, digits or underscore, and the second pattern does not require such heavy escaping,[()%%+={},:*^-]
is enough (only%
needs escaping here, as the-
is placed at the end of the character set and is thus treated as a literal hyphen).See this Lua demo: