0

In .NET, I'm trying to parse strings like these into groups of numbers. The "a" and "b" are optional (but fixed) letters:

111a222b333   --> groups: num1=111, num2=222, num3=333
111           --> groups: num1=111
111b333       --> groups: num1=111, num3=333
b333          --> groups: num3=333

The regular expressions I've tried include:

(?<num1>\d+)?a?(?<num2>\d+)?b?(?<num3>\d+)?
(?<num1>\d+)*.*(a(?<num2>\d+))*.*(b(?<num3>\d+))*

But they are not working. Any suggestions?

Glen Little
  • 6,951
  • 4
  • 46
  • 68
  • Your first regex begins with a superfluous '('. – JosefScript Mar 09 '17 at 17:19
  • "they are not working" - in what way are they not working? Are they giving you errors? are they not matching anything? are they returning the wrong things? Be more specific in what problems you've had. Providing standalone copy and paste runable code in a minimal complete verifiable example helps a lot (http://stackoverflow.com/help/mcve). – Chris Mar 09 '17 at 17:20
  • 1
    The second one never reaches group two and three because of the dot with asterisk being greedy. – JosefScript Mar 09 '17 at 17:22

3 Answers3

2

You need combine the ? (zero or one of) so to speak in a group (not a capture group).

Thus turning this:

a?(?<num2>\d+)

Into:

(?:a(?<num2>\d+))?

The full regex would then be:

(?<num1>\d+)?(?:a(?<num2>\d+))?(?:b(?<num3>\d+))?

Here's a live preview.

As you can see it correctly yields:

vallentin
  • 23,478
  • 6
  • 59
  • 81
1
(?<num1>\d*)?a?(?<num2>\d*)?b?(?<num3>\d*)

You were close, just needed to cover the case when the digit isn't there.

Preview

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
-1

This is a similar post: Regular expression to match any character being repeated more than 10 times

/([0-9])\1*/ should match what you're looking for, since in .NET, quantifiers are greedy by default.

Community
  • 1
  • 1
Explorer
  • 1
  • 1