开发者

A regular expression for matching a simple word in C#?

i need a regular expression to match only the word's that match the following conditions. I am using it in my C# program

  1. Can be any case
  2. Should not have any numbers
  3. may contain - and ' characters, but 开发者_C百科are optional
  4. Should start with a letter

I have tried using the expression ^([a-zA-Z][\'\-]?)+$ but it doesn't work.

Here are list of few words that are acceptable

  1. London (Case insensitive)
  2. Jackson's
  3. non-profit

Here are a list of few words that are not acceptable

  1. 12london (contains a number and is not started by a alphabet)
  2. -to (does not start with a alphabet)
  3. to: (contains : character, any special character other that - and ' is not allowed)


^[a-zA-Z][-'a-zA-Z]*$

This matches any word that starts with an alphabetical character, followed by any number of alphabetical characters, - or '.

Note that you don't need to escape the - and ' when it's inside the character [] class, as long as the dash is either the first or last character in the sequence.

Note also that I've removed the round brackets from your example - if you don't want to capture the input, you'll get better performance by leaving them out.


Try this one:

^[A-Za-z]+[A-Za-z'-]*$


First of all, try your regexes against tools such as http://www.regextester.com/

You are testing strings that both start with AND end with your pattern (^ means start of line, $ is the end), thus leaving out all of the words contained between two spaces.
You should use \b or \B.
Instead of looking for [a-zA-Z] you can use character classes such as '\D' (not digit).
Let me know if the above is working in your scenario.

\b\D[^\c][a-zA-Z]+[^\c]  

It says: word boundaries with no digits, no control characters, one or more alphabetical lower or uppercase character, with no following control characters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜