Anon
Anon

Reputation: 221

Java regex to check that string contains more than 2 words and has digits

I am writing a regex to match the string containing more than 2 words and should have at least 1 digit available or 1 word with no digits.

i.e If I have following strings:

1. "Sample data with no digit" (no digit)
2. "1004" (less than 2 words)
3. "1004 1008" (no alphabets)
4. "1004 data" (exactly 2 words)
5. "5ample Data with digits" (note that S-> 5)
6. "Sample Data with 1004"

The regex should match the 5th,6th strings (reason for not fetching others is mentioned along with the data)

I tried following but the following always returns all the strings:

[\d[0-9]|[ABEGFIHKJLOQPSRUTXZbgfihkjloqpsuz!]]+[\w\s]* (returns all strings)

Please note that I am using JAVA.

Please help and thanks in advance.

Upvotes: 1

Views: 1192

Answers (1)

anubhava
anubhava

Reputation: 786291

You can use this regex with 2 lookahead assertions:

^(?=.*\b[a-zA-Z]*\d+[a-zA-Z]*)(?=.*\b[a-zA-Z]+\b)(?:\w+\h+){2,}\w+

RegEx Demo

RegEx Breakup:

  • (?=.*\b[a-zA-Z]*\d+[a-zA-Z]*): Lookahead to ensure we have a word with a digit
  • (?=.*\b[a-zA-Z]+\b): Lookahead to assert we have a word with no digit
  • (?:\w+\h+){2,}\w+: Make sure we have at least 3 words in input

Upvotes: 1

Related Questions