Rohit Kumar Singh
Rohit Kumar Singh

Reputation: 669

Regex only for alphanumeric not for numbers

I want to match "3 T1F ROHITE01WMILWWI16" which contains only caps letter and numbers nothing else. The condition is that it ,must be both. It should not return true for all alphabetic and all numeric.

Test cases:

I am using ^[0-9 A-Z]+$ it matches correctly the first and last test case but also returns true for fourth test case i.e 1234.

Upvotes: 4

Views: 1937

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627537

You can use

^([0-9 A-Z]*[A-Z][0-9 A-Z]*[0-9][0-9 A-Z]*|[0-9 A-Z]*[0-9][0-9 A-Z]*[A-Z][0-9 A-Z]*)$

It matches

  • ^ - start of string

    Then come two alternatives. Alternative 1:
  • [0-9 A-Z]* - 0+ allowed characters
  • [A-Z] - one uppercase ASCII letter (obligatory subpattern)
  • [0-9 A-Z]* - 0+ allowed characters
  • [0-9] - a digit
  • [0-9 A-Z]* - 0+ allowed characters

    Alternative 2 is the same as above but the digit and uppercase letter character classes are swaped to match 1A or A1 strings.
  • $ - end of string

Thus, the minimum required string length is 2 characters.

See the regex demo and the IDEONE demo:

s <- c("3 T1F ROHITE01WMILWI16", "3 T1F ROHITE01WMILwI16", "3 T1F ROHITE01WMIL.I16", "1234", "aaaa", "T1F ROHITH01WMILWI16")
grep("^[0-9 A-Z]*[A-Z][0-9 A-Z]*[0-9][0-9 A-Z]*$", s, value=TRUE)

If you need to support strings of any length, use this PCRE regex:

^(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9])[0-9 A-Z]*$

The (?=[^A-Z]*[A-Z]) lookahead requires at least one uppercase letter and (?=[^0-9]*[0-9]) requires one digit.

See the IDEONE demo:

s <- c("3 T1F ROHITE01WMILWI16", "3 T1F ROHITE01WMILwI16", "3 T1F ROHITE01WMIL.I16", "1234", "aaaa", "T1F ROHITH01WMILWI16")
grep("^(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9])[0-9 A-Z]*$", s, perl=TRUE, value=TRUE)

Upvotes: 3

Adam
Adam

Reputation: 5253

You can use a lookahead:

^(?=.*[0-9])(?=.*[A-Z])[0-9 A-Z]+$

str1 <- c("3 T1F MILWWIHE01WMILWWI16", "3 T1F ROHITE01WMILWI16", "3 T1F ROHITE01WMILwI16", "3 T1F ROHITE01WMIL.I16", "1234", "aaaa", "T1F ROHITH01WMILWI16")

grep("^(?=.*[0-9])(?=.*[A-Z])[0-9 A-Z]+$", str1, perl=TRUE, value=TRUE)
# [1] "3 T1F MILWWIHE01WMILWWI16" "3 T1F ROHITE01WMILWI16"    "T1F ROHITH01WMILWI16"

Upvotes: 2

Cath
Cath

Reputation: 24074

Based on the now deleted answer by @akrun, you can look for strings that contain both uppercase letters and numeric characters (with or without spaces) and that don't contain any other characters:

str1 <- c("3 T1F MILWWIHE01WMILWWI16", "3 T1F ROHITE01WMILWI16", "3 T1F ROHITE01WMILwI16", "3 T1F ROHITE01WMIL.I16", "1234", "aaaa", "T1F ROHITH01WMILWI16")

str1[grepl("[A-Z ]+", str1) & grepl("[0-9 ]+", str1) & !grepl("[^A-Z0-9 ]", str1)]
# [1] "3 T1F MILWWIHE01WMILWWI16" "3 T1F ROHITE01WMILWI16"    "T1F ROHITH01WMILWI16"

Upvotes: 2

Related Questions