Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to write regex for matching Reference # 11223344 for .net

sample data

Reference # 12345678

Regex I have tried this regex:

(?<=Reference #)[ ]\d{8}\b

I am getting space inside the match, how to remove it from the match and put in the look behind instead.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In PCRE (the option you used at the regex testing online Web site), you can use

Reference\s+#\s+\K\d{8}\b

See the regex demo.

In .NET, you can use

(?<=Reference\s+#\s+)\d{8}\b

See this regex demo.

Details:

  • (?<=Reference\s+#\s+) – a positive lookbehind that matches a location that is immediately preceded with Reference + one or more whitespaces, # and then one or more whitespaces
  • Reference – a word
  • \s+#\s+ – a # char with one or more whitespaces both on the left and right
  • \K – a match reset operator that discards the text matched so far from the overall match memory buffer
  • \d{8} – eight digits
  • \b – a word boundary (you might want to replace it with a right-hand digit boundary, (?!\d), if you plan to get a match if the eight digits are bordering on a letter or underscore).
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading