I’m trying to detect 7 digit numbers from plain text and this is the regular expression I’m using.
Regex sevenDigit = new Regex(@"(?<!\d)\d{7}(?!\d)");
Now, I want to be able to match only those 7 digit numbers, that do not begin with a particular prefix. Specifically “usr_id”.
How can I modify this regex to match only those that are not of the form usr_id=1234567 ?
Thanks!
I would do this:
Note also the
\bword boundaries which are a more elegant way of saying “start of number” and “end of number”. They don’t match exactly like your(?<!\d)and(?!\d)lookaround assertions (most notably, they would not allow1234567to match withinabc1234567xyzwhich your regex would allow to match). So if you really need this approach, you could also do