0

I'm debugging some Java code that uses RegexFileFilter from Apache Commons IO with the following regex:

(?:custpartnum).*(?:8798518684708).*

This matches the following strings:

custpartnum-en-8798518684708.csv_20180612152233580
custpartnum-en-8798518684708.csv_filtered_20180612152300971

I'm trying to tell it to not match on the string containing filtered?

I tried this but it still matched both strings:

(?:custpartnum).*(?:8798518684708).*(?!filtered)
A_B
  • 823
  • 2
  • 11
  • 28
  • "Not Contain" is very tricky to achieve in Regular Expression. Consider matching for the filter and handling the negation in your java program code. https://stackoverflow.com/questions/406230/regular-expression-to-match-a-line-that-doesnt-contain-a-word – The Lyrist Jun 12 '18 at 21:55

2 Answers2

0

You can match files with only digits after the .csv :

custparnum[^.]*8798518684708.csv_\d*

I took the opportunity to remove your (?:non-capturing groups) that likely serve no purpose (they'd be useful if you had to modify them with a quantifier).

Aaron
  • 21,986
  • 2
  • 27
  • 48
0

You could move the negative lookahead to before the .* part and update it to match any character zero or more times followed by filtered.

This way you are first asserting using the negative lookahead that after you have matched 8798518684708 there should not be filtered on the right side.

(?:custpartnum).*(?:8798518684708)(?!.*filtered).*

You might use an updated version without the non capturing groups:

custpartnum.*8798518684708(?!.*filtered).*

The fourth bird
  • 96,715
  • 14
  • 35
  • 52