Home > database >  How to capture a regex group after the specific word?
How to capture a regex group after the specific word?

Time:07-25

I am stuck with regex again. I have this example:

 3, 
SomethingThatShouldntMatch: 3, 5
Divisions: 1, 2, 3, 5, 13
Divisions: 33
Divisions: 3, 22
Divisions: 4, 55
Divisions: 10, 31

I want to find lines beginning with "Divisions:" and only in these lines capture the first specified number.

For example, if I put number "3" to search pattern it should return me only lines beginning with "Divisions:" and number 3 somewhere after the word.

Not 13, not 33, not 30. Only 3 or nothing. This pattern is planned to use in Regex.Replace().

The main problem I've ran into is that it won't continue searching if the specified number is not first after the word. "Divisions: 1, 2, 3" won't match but it should.

However, "Divisions: 3, 5" matches. I've tried about a dozen of expressions but I can't figure it out. What am I doing wrong? Why it doesn't search after the first comma?

[\n\r].*Divisions:?(\D3\D)?

enter image description here

I hope this works for all your cases :)

  • Related