Search code examples
c#regexregex-lookaroundslookbehind

Regex simultaneous lookahead and lookbehind


I am trying to write a Regex that simultaneously matches pattern in the front and pattern in the back of my current character.

E.g.:

red, gree, yellow-white, snowy

in

312415 red, gree, yellow-white, snowy CDEFASE

one, two, three #four

in

29321093123 one, two, three #four IRUASJDJADALLDLA

As you can see, in front and in the back of the middle parts of the string I have the same pattern:

front: \d+

back: [A-Z]+

How can I accomplish that?


Solution

  • Use the below regex and get the string you want from group index 1.

    \d+\s+(.*?)\s+[A-Z]+
    

    DEMO