On Tue, 2025-03-11 at 22:12 +0000, Roger L Costello costello@xxxxxxxxx
wrote:
> Liam gave this wicked cool way to return the portion of a string that
> matches a regex pattern:
>
> replace($input, '^.*(\d+).*$', '$1')
>
> However, I did some testing, and it's not returning the desired
> results:
Changing the initial .* to .*? (non-greedy) will fix this, as the .
will no longer match a digit and eat away at the integer.
This is because the .* is greedy by default, and eats as much of the
start of the string as possible, leaving only one digit for the \d (or
[0-9] if you prefer).
My bad, i should have tested before posting, sorry!
In this case, ^[^\d]*(\d+).*$' would be a variant that is more obvious,
but that doesnt work for a general pattern.
If you use the input 'The person put 203 cookies into 3 jars'
you can see the .* approach will return 3 and the .*? will return 203.
liam
--
Liam Quin,B https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processin'g/A11Y tra'ining, work & consulting.
Barefoot Web-slave, antique illustrations: B http://www.fromoldbooks.org
|