Search code examples
regexregex-lookarounds

Regex: Match partial string


I need some help - my skills here falls short :) (and I don't know if it is possible with pure regex)

Case: I have some text inputs in the form of:

input1: "abc,clutter,01;xyz,clutter,02;" (should match)
input2: "abc,clutter,02;zyz,clutter,01;" (no match)
input3: "abc,clutter,02;abc,txt,txt,01;xyz,clutter,01" (should match)

Then match should be

  • Starts with abc (anywhere in the input)
  • Everything in between - unless ,02; is in-between
  • Ends with ,01;

So something like: abc(.*)(?!,02;),01; .. but this also matches input2, and that was not the intension :)


Solution

  • You might use for example a repeating pattern matching all chars except , and ;

    \babc(?:,(?!02,)[^,;\n]+)*,01;
    
    • \babc A word boundary, match abc
    • (?: Non capture group
      • ,(?!02,)[^,;\n]+ Negative lookahead, assert not 02, and match any char except , ; or a newline
    • )* Close the group and optionally repeat
    • ,01; Match literally

    Regex demo

    If abc should only be matched one, you can also add that to the negative lookahead

    \babc(?:,(?!(?:02|abc),)[^,;\n]+)*,01;
    

    Regex demo