Regex Template Help

I will have the following possible lines:

12_3

or

12_3 14_1 + + 16_3-400_2

Numbers may be different, but what I'm looking for is X_X numerical patterns. However, I need to make a replacement that will look for 2_3 and NOT return 12_3 as a valid match.

The +/- characters are artemic characters and can be any valid value. They are also required by ARENT (in the example of the first) .. so that I could check a line that has only 12_3, and if I go to 2_3, this will NOT return a match. Only if I went through 12_3.

This is for a C # script.

Thanks so much for any help !! I'm regex stupid.

+3
source share
2 answers

, , ..: 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);

:

[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);

:

[0] [0] = 12_3

[1] [0] = 14_1

[2] [0] = 16_3

[3] [0] = 400_2

, ?

+2

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

\d - , \b - . . # regex cheat sheet.

UPDATE: " # regex", , \b ( \< \> grep). . . , -.

+2

Source: https://habr.com/ru/post/1781772/


All Articles