none
Regex that should be easy... maybe?? RRS feed

  • Question

  • Not very experienced with regex, but i was thinking this should be pretty easy, but is turning out to be too much.  I will have some text in a format like the following (bold just denotes the separation of each repeating piece text)

    Error Code XXXXX: some text here Error Code XXXXX: more text here Error Code XXXXX: this is the 3rd text string

    I want group1 of each match to be the XXXXX value which can be expressed with the following: ((?:[A-Z]|[0-9]){1,10}): 

    I want group2 of each match to be the text between "XXXXX: " and the next "Error Code" (or end of line)

    The following produces the two group1 matches I want:

    So now I need to add something to capture the text as group2. I've tried the following, which captures the 2 groups just how I want them, but ONLY for the first error code occurrence

    How can I get that to repeat and work for any number of occurrences of the repeating text?

    Thanks in advance for any insight!

    Sunday, December 15, 2019 12:07 AM

All replies

  • I was finally able to figure this out by moving the lazy quantifier inside of the capture group:

    ((?:[A-Z]|[0-9]){1,10}): (.+?)(?=Error Code|$)

    I also:

    • added the end-of-line anchor to account for last repeating text occurrence
    • removed the "Error Code" from front-end... didn't seem to make a difference either way

    Sunday, December 15, 2019 4:59 AM
  • Hi JeremyRuth,

    Thank you for posting here.

    Try the following regex:

        Error Code ((?:[A-Z]|[0-9]){1,10}):(.*?) Error\sCode

    Here's a result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 16, 2019 7:07 AM
    Moderator