34

I have requirement to allow alphanumeric and certain other characters for a field. I am using this regular expression:

 "^[a-zA-Z0-9!@#$&()-`.+,/\"]*$".

The allowed special characters are! @ # $ & ( ) - ‘ . / + , “

But when I test the pattern with a string "test_for_extended_alphanumeric" , the string passes the test. I don't have "_" allowed in the pattern. What am I doing wrong?

7 Answers 7

51

You need to escape the hyphen:

"^[a-zA-Z0-9!@#$&()\\-`.+,/\"]*$"

If you don't escape it then it means a range of characters, like a-z.

0
6

In your character class the )-' is interpreted as a range in the same way as e.g. a-z, it therefore refers to any character with a decimal ASCII code from 41 ) to 96 '.

Since _ has code 95, it is within the range and therefore allowed, as are <, =, > etc.

To avoid this you can either escape the -, i.e. \-, or put the - at either the start or end of the character class:

/^[a-zA-Z0-9!@#$&()`.+,/"-]*$/

There is no need to escape the ", and note that because you are using the * quantifier, an empty string will also pass the test.

4

Using this regex you allow all alphanumeric and special characters. Here \w is allowing all digits and \s allowing space

[><?@+'`~^%&\*\[\]\{\}.!#|\\\"$';,:;=/\(\),\-\w\s+]*

The allowed special characters are ! @ # $ & ( ) - ‘ . / + , “ = { } [ ] ? / \ |

0
1

Hyphens in character classes denote a range unless they are escaped or at the start or end of the character class. If you want to include hyphens, it's typically a good idea to put them at the front so you don't even have to worry about escaping:

^[-a-zA-Z0-9!@#$&()`.+,/\"]*$

By the way, _ does indeed fall between ) and the backtick in ASCII:

http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters

1
  • 3
    what if I want to allow the (ü é â ä à å ç ê ë è ï î ì æ Æ ô ö ò û ù ÿ á í ó ú) character in input with regex. Apr 7, 2021 at 14:13
0

How about this.. which allows special characters and as well as alpha numeric

"[-~]*$"
0

Because I don't know how many special characters exist, it is difficult to check the string contains special character by white list. It may be more efficient to check the string contains only alphabet or numbers.

for kotlin example

fun String.hasOnlyAlphabetOrNumber(): Boolean {
    val p = Pattern.compile("[^a-zA-Z0-9]")
    return !(p.matcher(this).matches())
}

for swift4

func hasOnlyAlphabetOrNumber() -> Bool {
    if self.isEmpty { return false }
    do {
        let pattern = "[^a-zA-Z0-9]"
        let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
        return regex.matches(in: self, options: [], range: NSRange(location: 0, length: self.count)).count == 0
    } catch {
        return false
    }
}
0

Regex sucks. Here is mine

/^[a-zA-Z\d-!@#$%^&._"'()+,/;<>=|?[]\`~{}]$/

Mine is a little different than others but it is more self explanatory. You use \ in front of any special symbol like ] or . I had issues with -, , and ] so I had to put ], \, and move the - to the left. I also had issues with | but I moved it left and it fixed it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.