views:

269

answers:

6

In my controller, I current set a constraint that has the following regex:

 @"\w+"

I want to also allow for underscore,dash and period.

THe more effecient the better since this is called allot.

any tips?

+1  A: 

(?:\w|[-_.])+

Will match either one or more word characters or a hyphen, underscore or period. They are bundled in a non-capturing group.

Skurmedel
+1  A: 

try this:

@"[._-\w]+"
deerchao
A: 

Does the following help? @"[\w_-.]+"

P.S. I use Rad Software Regular Expression Designer to design complex Regexes.

ironic
A: 

@"[\w_-.]+"

im no regex guru was just a guess so verify that it works...

Petoj
A: 

I'd use @"[\w\-._]+" as regex since the dash might be interpreted as a range delimiter. It is of no danger with \w but if you later on add say @ it's safer to have the dash already escaped.

There's a few suggestions that have _-. already on the page and I believe that will be interpreted as a "word character" or anything from "_" to "." in a range.

Don
A: 

Pattern to include all: [_.-\w]+

You can suffix the _ \. and - with ? to make any of the characters optional (none or more) e.g. for the underscore:

[_?\.-\w]+

but see Skurmedel's pattern to make all optional.

Dave Everitt