My regex string looks something like
Your order #Q-111-111-1111. Bill amount is $100.50
How can I extract order number i.e. string between '#' and first '.'
Q-111-111-1111
I've tried:
/#(.*[^.])[.].+?/
But it's capturing upto the last dot character. I'm sure, I need to use non greedy matching but I'm unable to accomplish that.
You can simply use the following regex:
#(.*?)\.
* is greedy - it causes the engine to repeat the preceding token as often as possible, when you append ? to .*, it'll be lazy (ungreedy), and will match until the first dot.
To demonstrate it, lets take an example:
#(.*)\. applied on #12-34.234-3.234. hello world
.* will first match everything until the end of the string (d character), then it'll try to match dot, but it doesn't exist, so it'll keep backtracking untill it matches 4, then the dot is found, matched string is #1234.2343.234
#(.*?)\. applied on #12-34.234-3.234. hello world
.*? will match only until 4, then the dot is found and will be matched, the laziness causes it to stop searching for more tokens to consume
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With