3

I am not sure if the title makes sense but I was not sure how to word it. I have strings (of filenames) that looks like

/aa/john/doe/xx/yy/xxTRUEyy.jar
/bb/ee/john/doe/xx/yy/aaTRUE.jar
/cc/john/doe/xx/yy/aaFALSE.jar
/dd/john/deere/xx/yy/aaTRUE.jar

I need a regex that does NOT match strings that HAVE /john/doe/ in them, AND have a jar file with TRUE as part of the name. (In the above examples, that should match only string 3 and 4).

3
  • Why do you need negative lookahead? Apr 2, 2015 at 13:31
  • CommuSoft, thanks for the response - however I misstated the problem - which i have edited - sorry about that. Apr 2, 2015 at 13:47
  • As worded, your criteria would only match the last string, which the by @Willem Van Onsem correctly matches. You should edit your question to remove the spurious claim of string number three matching or improve your specification to match what you are trying to achieve.
    – rivy
    Dec 27, 2015 at 6:46

1 Answer 1

4

You can use a (?!) group to perform negative lookahead:

^(?!.*\/john\/doe\/).*TRUE[^\/]*\.jar$

should be sufficient. regex101 demo.

3
  • You are right - i dont need a lookahead - however thats because i missated the problem which i have now edited. So sorry about that. Apr 2, 2015 at 13:50
  • @user1862830: modified, better? Apr 2, 2015 at 13:53
  • 1
    Nice! The trick to the question on the title was moving the wildcard inside the negative lookahead. So instead of /.*(?!joe)/ we would do /(?!.*joe)/
    – Ricardo
    Jan 25 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.