Search code examples
c#regexregex-group

How to capture a regex group after the specific word?


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)?

https://regex101.com/r/ydqLGB/1


Solution

  • The pattern [\n\r].*Divisions:?(\D3\D)? does not match because it matches Divisions followed by an optional colon and then directly after is optionally matches a 3 char surrounded by non digits.

    The only string where that matches in the examples is Divisions: 3, as the 3 is directly following and the \D matches the space before it and the comma after it.

    Note that \D actually matches a character and the [\n\r] matches a mandatory newline.


    If a single line like Divisions: 3 should also match, you might write the pattern as:

    ^Divisions:[ ,0-9]*\b(3)\b
    
    • ^ Start of string
    • Divisions: Match literally
    • [ ,0-9]* Match optional spaces, comma or digit 0-9
    • \b(3)\b Capture a 3 char between word boundaries

    See a regex demo


    If the colon is optional:

    ^Divisions:?[ ,0-9]*\b(3)\b