1
votes

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.

1
What about "Everest, Eiger, K2", i.e. what do you mean by a word?Bathsheba
try this ([0-9]|[a-zA-Z]){2,}\s*Lino
Yes, it is a valid string and it should be fetched. A word can be considered whenever we get a space. So, in this we have 3 words.AMANDEEP SINGH
@Lino, The regex shared is matching "data" String and not "data data 2"AMANDEEP SINGH
"should have at least 1 digit available or 1 word with no digits" - this does not make sense, as any alphanumeric string meets this requirement.Tom Wyllie

1 Answers

1
votes

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