Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the digits before some particular word using regex in c#?

Tags:

c#

.net

regex

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.

like image 831
bala k Avatar asked Dec 13 '19 06:12

bala k


3 Answers

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

like image 132
CinCout Avatar answered Sep 24 '22 08:09

CinCout


Presuming that "anything" does not include digits, you could use this regex:

(\d+)[^\d]+someWord

Demo on regex101

like image 23
Nick Avatar answered Sep 23 '22 08:09

Nick


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

like image 42
Steve Chambers Avatar answered Sep 21 '22 08:09

Steve Chambers