Yoav
Yoav

Reputation: 2077

Regular expression - extract 6 digits exactly

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

Answers (6)

Arwin
Arwin

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

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45721

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:

  1. 123365
  2. 123365

123365text text text 234098 text text text text text text text text 567890 text123365

Matches:

  1. 123365
  2. 234098
  3. 567890
  4. 123365

Upvotes: 6

Ian Hughes
Ian Hughes

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

David Paxson
David Paxson

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

BlueMonkMN
BlueMonkMN

Reputation: 25601

System.Text.RegularExpressions.Regex re = 
    new System.Text.RegularExpressions.Regex(@"(^|\D)(\d{6})($|\D)");

Upvotes: 6

Dan Tao
Dan Tao

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

Related Questions