Suryateja Kosaraju
Suryateja Kosaraju

Reputation: 513

RegEx for including alphanumeric and special characters

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?

Upvotes: 35

Views: 170795

Answers (7)

Evan Erickson
Evan Erickson

Reputation: 501

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.

Upvotes: 0

shubham
shubham

Reputation: 41

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 ! @ # $ & ( ) - ‘ . / + , “ = { } [ ] ? / \ |

Upvotes: 4

Brownsoo Han
Brownsoo Han

Reputation: 4701

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
    }
}

Upvotes: 0

Karthik Cherukuri
Karthik Cherukuri

Reputation: 633

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

"[-~]*$"

Upvotes: 0

MikeM
MikeM

Reputation: 13631

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.

Upvotes: 6

Explosion Pills
Explosion Pills

Reputation: 191749

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

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838256

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.

Upvotes: 54

Related Questions