开发者

Regex to match spaces and apostrophes

I need a Regex that matches all instances of开发者_StackOverflow社区 any character that is not a-z (space and things like apostrophes need to be selected). Sorry for the noob factor.

//novice


With a somewhat sophisticated regex engine (grep will do just fine) this will be quite general:

/[^[:lower:]]+/

(Note the ^!)

The difference between [:lower:] and [a-z] is that the former should be I18N friendly and match e.g. ü, â etc.

For case insensitive matching use [:alpha:], to also include digits use [:alnum:]. [:alnum:] differs from \W in that it doesn't include _ (underscore).

Note that character classes written in this style may be combined as usual (like a-z etc.), e.g. [^[:lower:][:digit:]]+ would match a non-empty string of characters not including any lowercase letters or digits.


Here is regex that will literally match any char that is not a-z. The /g flag indicates a global match which will cover all instances of the match.

/[^a-z]+/g

If you need uppercase letters too, you can either pass the /i flag which indicates case insensitivity:

/[^a-z]+/gi

or include the uppercase chars in character class:

/[^a-zA-Z]+/g


The character class [^a-zA-Z] will match any character that isn't (upper or lowercase) a-z.

I'm sure you can figure out the rest.


\W will match any non-alphanumeric (a-z, 0-9, and underscore) character.


The following regular expression matches any letter other than [a-z]:

/[^a-z]+/


OK.

  1. /[^a-z]+/ will match anything other than lowercase letters.
  2. /[^A-Za-z]+/ will match anything non-alpha.
  3. /\W+/ on most systems will match non-'word' characters. Word characters include A-Z, a-z, 0-9, and '_' (underscore). Note that that is an uppercase W.


If you ever need to create another regex try reading this. Teaching to fish and all that. :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜