My Regex :


To Match following string:


The above works okay...but if I want to match a string when it is not equal to (004) bolded. I tried negative lookahead (?!004) it doesn't match the string



Am I missing something

  • 1
    What regular expression engine are you matching with? Is it grep or something else? If it's in some other programming language, then it may be off-topic here. – Kusalananda Apr 13 '18 at 14:42

Based on your pattern and answer from https://stackoverflow.com/questions/1971738/regex-for-all-strings-not-containing-a-string: the following pattern work in my editor: ^[0-9]{9}_(010020).*(?<!004)-[0-9,a-z,A-Z]+_[0-9]{8}_[0-9]{4}

I think there is an extra underscore in your pattern after the '+' sign. And we have to match the last 3 character before checking they are not "004").

Depending on the exact formatting you might want to ensure we are only matching 3 digits: ^[0-9]{9}_(010020)([0-9]*(?<!004)){3}-[0-9,a-z,A-Z]+_[0-9]{8}_[0-9]{4}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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