3

I will have the following possible strings:

12_3

or

12_3+14_1+16_3-400_2

The numbers could be different, but what I'm looking for are the X_X numeric patterns. However, I need to do a replace that will search for 2_3 and NOT return the 12_3 as a valid match.

The +/-'s are arthemtic symbols, and can be any valid value. They also ARENT required (in the example of the first) .. so, I might want to check a string that just has 12_3, and if I pass in 2_3, it would NOT return a match. Only if I passed in 12_3.

This is for a C# script.

Many thanks for any help!! I'm regex stupid.

2
  • 2
    I don't understand the plethora of "questions" that are nothing more than requests for us to do your work for you. There are a large number of ways to learn Regular Expressions from reading great books like Mastering Regular Expressions or software such as RegEx Buddy. Either way there is no excuse for asking a Regular Expressions question without showing any expressions you've tried to craft first. Commented Dec 23, 2010 at 3:03
  • @Frazell - this is the fifth question by this user, and his/her first regex question. If you think there's a general problem please start a question on meta, but it's basically noise here. Commented Dec 23, 2010 at 5:10

2 Answers 2

2

Ok, we have ,i.e.: 2_3+12_3+14_1+16_3-400_2+2_3

regexp #1:

Regex r1 = new Regex(@"\d+(\d_\d)");
MatchCollection mc = r1.Matches(sourcestring);

Matches Found:

[0][0] = 12_3 [0][1] = 2_3

[1][0] = 14_1 [1][1] = 4_1

[2][0] = 16_3 [2][1] = 6_3

[3][0] = 400_2 [3][1] = 0_2

regexp #2:

Regex r2 = new Regex(@"\d+\d_\d");
MatchCollection mc = r2.Matches(sourcestring);

Matches Found:

[0][0] = 12_3

[1][0] = 14_1

[2][0] = 16_3

[3][0] = 400_2

Is here that what you were looking for?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this! I had a similar pattern to your second one, but I couldnt figure out if I needed to do multiple patterns based on the digit count on the left and right side of the _ .. Very helpful!
2

\b\d+_\d+\b.

\d is digit and \b is a zero-width word boundary. See this C# regex cheat sheet.

UPDATE: I just looked for a "C# regex cheat sheet" to verify that \b was a word boundary (it's \< and \> in grep). That was the first result. I didn't actually verify the text. Anyway, I now link to a different cheat sheet.

4 Comments

See, I know what numbers I'm looking for.. So, for example, I know in my string I'll be looking for 2_3 .. So when I use the pattern \b2_3 , it finds the right "2_3" in the string 12_3+2_3+122_3 .. I don't really understand why, though?! :)
"Matches at the position between a word character" .. I'm guessing it sees 12_3 as a word, and the + as a delimiter?
Don't mind that cheat sheet. \b matches a position that's either followed by a word character and not preceded by one (start of word), or preceded by a word character and not followed by one (end of word). The other character (if there is one) can be whitespace, punctuation, or any character that's not a "word character" (i.e., a letter or digit, or connecting punctuation like the underscore). \b2_3 doesn't match 12_3 because the \b would have to match between the two word characters, 1 and 2.
@extreme: If you know your numbers, you should be able to do this: \b2_3\b.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.