tags:

views:

457

answers:

6

Hi,

I need a regex that allows only alphanumeric plus the + and - character.

Right now I am using:

[^\w-]
+1  A: 

Try this :

[a-zA-Z0-9+\-]
Canavar
your forgetting 0-9 right?
mrblah
No, alphanumeric...
David M
Yes guys, I just updated.
Canavar
+1  A: 

You have to escape the - char: [\w\-+] for single character and [\w\-+]+ for more.

streetpc
That will allow for non-alphanumerics as well; it only indicates that there are alpanumerics and '+' or '-' in the string, but does not exclude other characters.
Fredrik Mörk
No you don't, just put the "-" first
Draemon
Putting special characters first/last works, but it's worth showing explicitly that it's a special character, so that it doesn't accidentally "activate" when the pattern is modified.
Blixt
A: 

[a-zA-Z0-9\+\-]

David M
+3  A: 

This regular expression will match only if you test it against a string that has alphanumeric characters and/or +/-:

^[a-zA-Z0-9\-+]+$

To use it:

if (Regex.IsMatch(input, @"^[a-zA-Z0-9\-+]+$"))
{
    // String only contains the characters you want.
}
Blixt
It does not match the string "äbc" though...
Fredrik Mörk
I always feel that "0-9" is better expressed as "\d". +1, because this is the most complete answer yet.
Cerebrus
@Fredrik: True, it does of course depend on which language it's for. Using `\w` isn't correct either though, due to the fact that it includes the `_` character.
Blixt
@Cerebrus: Yeah, I guess that's nice too, although I like the explicity of `0-9`. I mostly use `\d` when I only want to match `0-9` outside of a character class, because it's much more concise than `[0-9]`. Inside a character class, there's only a one character difference.
Blixt
There's more of a difference than that. .NET's `\d` is the same as `\p{Nd}`, which matches a lot more than `[0-9]` (but you can change that with the `RegexOptions.ECMAScript` modifier).
Alan Moore
+1  A: 

Matches single -, + or alpha-numeric:

[-+a-zA-Z0-9]

Matches any number of -, + or alpha-numeric:

[-+a-zA-Z0-9]*

Matches a string/line of just -, + or alpha-numeric:

^[-+a-zA-Z0-9]*$
Draemon
+5  A: 

The following pattern will match strings that contain only letters, digits, '+' or '-', including international characters such as 'å' or 'ö' (and excluding the '_' character that is included in '\w'):

^[-+\p{L}\p{N}]+$

Examples:

string pattern = @"^[-+\p{L}\p{N}]+$";
Regex.IsMatch("abc", pattern); // returns true
Regex.IsMatch("abc123", pattern); // returns true
Regex.IsMatch("abc123+-", pattern); // returns true
Regex.IsMatch("abc123+-åäö", pattern); // returns true
Regex.IsMatch("abc123_", pattern); // returns false
Regex.IsMatch("abc123+-?", pattern); // returns false
Regex.IsMatch("abc123+-|", pattern); // returns false
Fredrik Mörk
Bummer ;o) forgot to exclude the underscore... There is always something you overlook when dealing with regex.
Fredrik Mörk
There, did some homework. Hopefully there are no other hidden suprises now.
Fredrik Mörk
+1 for using Unicode character points. =)
Blixt
@Blixt: yes, I like supporting international characters. Comes with being non-English perhaps. Thanks for pointing out the underscore issue, btw.
Fredrik Mörk
Of course, I'm Swedish too `=)` I just like the principle of starting with a limited solution, and extending it as the need arises (of course, the OP might very well want to support international characters from the start.) You could probably say I'm talking about the same thing as the *YAGNI* principle (http://en.wikipedia.org/wiki/YAGNI)
Blixt