Home > front end >  how to find a 9 digit number from the text with a regex
how to find a 9 digit number from the text with a regex

Time:01-03

I want to find regex to get 9-digit number which should not start with any non-numeric. code. if the 9-digit number starts with any character or symbol except number, it should not match.

sample data

** NOTIFICATION: FAX RECEIVED SUCCESSFULLY ** TIME RECEIVED REMOTE 112233445 A112211221 BMC123456789

code

var regex = new Regex(@"\d{9}");
var myCapturedText = regex.Match(str).Value;
Console.WriteLine("This is my captured text: {0}", myCapturedText);`

with existing code its taking the number,which starts with alphabets like a1122334415

desired op - 112233445

CodePudding user response:

Try this:

\b\d{9}\b

\b word boundary

See regex demo

CodePudding user response:

I just tried the following commandline and it worked:

grep -wo "\d{9}" file.txt
112233445

The switch -o means "shown only result" and -w means "whole word", and that last one is the clue: it's not about modifying your regular expression, which seems to be correct: it's about "grep"-ping your regular expression as a whole word, which might solve your issue.

  • Related