2

I want to write a regex to evaluate 12 character strings, a sample string is a02016ab-B30, I want to find all strings that have 4 to 6 character as 016 and 10 to 11 character not as B3.

.{3}(061).{3}(?!B3) doesn't exclude string with more than 12 characters? how can I refine it?

1
  • 1
    The string is unachored and matches 9 chars. Should 061 be 016? Commented Jul 10, 2020 at 15:41

1 Answer 1

1

You may use

^.{3}061.{3}(?!B3).{3}$

Or, if you have 016 after the first three chars:

^.{3}016.{3}(?!B3).{3}$

See the regex demo.

Details

  • ^ - start of string
  • .{3} - any three chars other than line break chars
  • 061 - a substring
  • .{3} - any three chars other than line break chars
  • (?!B3) - B3 substring is not allowed at the current location
  • .{3} - any three chars other than line break chars
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! That was an easy fix!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.