Reputation: 2077
Yoav - edited to be clearer
Hi, I need to find 6 digit numerical strings in a text file. I'm working in C#. Example:
text text text123365 text text text
The expression must skip strings longer then 6:
text text text1233656 text text text
The above string should not return any result because the length of the digit string is 7.
I came up with this expression: [^0-9]([0-9]){6}[^0-9]
It works perfectly with the exception of string that are at the start or end of the line
123365text text text text text text
text text text text text text123365
Is it possible to identify these cases?
Upvotes: 0
Views: 16715
Reputation: 7
We needed to match 6 digits, anywhere in a string (single line). This variation of the above is what ended up working for us:
(^|\b)(\d{6})(\b|$)
Upvotes: -1
Reputation: 45721
Try:
(?<!\d)\d{6}(?!\d)
It says:
It will look anywhere in the string.
Examples:
123365text text text text text text text text text text text text123365
Matches:
123365text text text 234098 text text text text text text text text 567890 text123365
Matches:
Upvotes: 6
Reputation: 401
I think you'd be better off using negative lookahead and lookbehind to do this rather than boundaries or not matches, like:
(?<![0-9])[0-9]{6}(?![0-9])
Upvotes: 5
Reputation: 553
Try this:
(^|[^0-9])(?<num>[0-9]{6})[^0-9]
This appears to match as you want. I tested it out using Regexdesigner
Upvotes: 0
Reputation: 25601
System.Text.RegularExpressions.Regex re =
new System.Text.RegularExpressions.Regex(@"(^|\D)(\d{6})($|\D)");
Upvotes: 6
Reputation: 128317
I was about to suggest the same thing as PatrikAkerstrand (\b\d{6}\b
). Instead, I'll post a link to a working example on Rubular.
Upvotes: 0