Home > Enterprise >  Regular Expressions. Find unknown length string excluding character combinations
Regular Expressions. Find unknown length string excluding character combinations

Time:08-05

I am new to regex and I spent half of my day to solve this question. I tried to search for answers, but couldn't find similar example.

I need to match strings if their end does not include char combinations such as (L|R|MIR)..


My tries:

  1. 1234.567.89.w001.*(?!R|L|MIR)\.dwg

  2. 1234.567.89.w001[^.]*(?!R|L|MIR)$\.dwg

and more..


Possible outcomes:

  1. 1234.567.89.w001.dwg TRUE
  2. 1234.567.89.w001 some random extensions.dwg TRUE
  3. 1234.567.89.w001-MIR some random text or no.dwg FALSE (MIR included after base name)
  4. 1234.567.89.w001_L.dwg FALSE (L included after base name)
  5. 1234.567.89.w001-R.dwg FALSE (R included after base name)
  6. 1234.567.89.w001R.dwg FALSE
  7. 1234.567.89.w001MIR.dwg FALSE

CodePudding user response:

Im not an expert at regex but when I tested this, outcomes 1 and 2 returned true, the rest returned false.

1234\.567\.89\.w001(?!.*(R|L|MIR)).*\.dwg

You were very close, you just needed to include the 'allow any character' .* inside the lookahead.

Also make sure you \ escape the . because in regex the . character means any character not the . itself.

If you're new to regex, try using https://regex101.com. Thats what I use to check mine.

  • Related