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?
Try:
(?<!\d)\d{6}(?!\d)
It says:
- Find 6 digits that are not directly preceeded or succeeded with a digit
It will look anywhere in the string.
Examples:
123365text text text text text text text text text text text text123365
Matches:
- 123365
- 123365
123365text text text 234098 text text text text text text text text 567890 text123365
Matches:
- 123365
- 234098
- 567890
- 123365
System.Text.RegularExpressions.Regex re =
new System.Text.RegularExpressions.Regex(@"(^|\D)(\d{6})($|\D)");
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])
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.
Try this:
(^|[^0-9])(?<num>[0-9]{6})[^0-9]
This appears to match as you want. I tested it out using Regexdesigner
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|$)
精彩评论