Chris Ballance
Chris Ballance

Reputation: 34337

I know this regex can be simplified - .NET

Here are my requirements:

The regex I have below works, but in particular I'm not sure how to reuse the character class [\w\(\)\.\-\[\]!#,&*+:'\/]

[\w\(\)\.\-\[\]!#,&*+:'\/][\w\s\(\)\.\-\[\]!#,&*+:'\/]{0,79}

Update:

Thanks for all your answers, this one did the trick

^(?!\s)[\w\s().\-!#&]{1,80}$

Upvotes: 0

Views: 149

Answers (4)

Geoff
Geoff

Reputation: 4746

If the first character can't be white space try this: (?!\s)[-\w\s().[\]\\!#,&*+:'/]{1,80}. You may want to "bracket" with ^ in the beginning and $ at the end ^(?!\s)[-\w\s().[\]\\!#,&*+:'/]{1,80}$, to have the regex match the whole string.

Upvotes: 3

Reuben Peeris
Reuben Peeris

Reputation: 541

You could use a negative look ahead to check the first character is not white space. Then there is no need to reuse a character class.

(?!\s)[\w\s\(\)\.\-\[\]!#,&*+:'\/]{1,80}

Upvotes: 2

Andomar
Andomar

Reputation: 238076

Inside a character class, only ] and \ need escapes. Even - doesn't need an escape if it's the first character of the class!

Here's the simplest regex I could reduce it to:

[- \w().[\]!#,&*+:'/]{1,80}

Upvotes: 3

developmentalinsanity
developmentalinsanity

Reputation: 6229

Does it really need to be 100% regex? Couldn't you just do

[\w\s\(\)\.\-\[\]!#,&*+:'\/]{1,80}

and separately verify that the first character isn't whitespace?

Upvotes: 4

Related Questions