0

I have already a regex (?:[a-zA-Z\d]+(?:[/][a-zA-Z\d]+)+) which matches correctly:

cb/09/06233/full 13/02513/MAJOR ADA/333

but also matches incorrectly

industrial/warehouse

How can I update my regex to exclude phrases without numbers?

I've tried (?=\d) to add somewhere inside but no effect.

1
  • You are close. Try adding (?=\S*\d) Demo Commented Mar 27, 2019 at 12:32

1 Answer 1

2

You could use a positive lookahead (?=[^\d\s]*\d) to assert what is on the right is not a whitespace char or a digit.

For that you could use a character class [^\d\s]* to match not a digit or a whitespace char and a quantifier to repeat that 0+ times. Then match a digit \d.

(?=[^\d\s]*\d)(?:[a-zA-Z\d]+(?:/[a-zA-Z\d]+)+)

Regex demo

Sign up to request clarification or add additional context in comments.

3 Comments

It's good but why it doesn't match fully ada/333 while matches ADA/333? I know it's not in above set.
Found a bug. Probably you forgot about slash at \d: (?=[^\d\s]*\d)
@Peter.k You are right, I forgot to escape the \d. Updated accordingly.

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.