We will use below regex to get the digits before the words.
Example :
838123 someWord 8 someWord 12 someWord
(\d+)\s*someWord
But sometimes anything will come between Number and word.Please see the below example line.
Ex:
43434 of someword 12 anything someword 2323 new someword
How to get the exact digit before that word using regex?
Please give me your suggestions.
Do this:
(\d+)[^\d]+some[wW]ord
You need to accept anything other than digits themselves.
Also I considered both w
and W
since your examples contained both.
Demo
Presuming that "anything" does not include digits, you could use this regex:
(\d+)[^\d]+someWord
Demo on regex101
One possible "missed corner case" from CinCout's answer is if the match for someWord
must be exact, e.g. if notsomeWord
and someWordNotThis
shouldn't be matched.
The following extension to that regular expression provides a way to address this:
(\d+)[^\d]*[^\w]some[wW]ord[^\w]
Explanation: The [^\w]
before or after the matcher for someWord
look for a "non-word character" before and after it - an end of the line also counts here. This could of course be made more complex/specific, depending on the exact requirements.
Demo
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