开发者

RegEx differences

Can someone please tell me the difference exactly between these 2 RegEx's?

'/[^a-zA-Z0-9\s]/'

and

'~[^A-Za-z0-9_]~'

Also, is there a syntax error for the space within the first Regex? Thinking it needs to be like开发者_StackOverflow中文版 this: /\s to be escaped properly.

Basically, I need a RegEx that only uses English A-Z, a-z, 0-9, and underscores only! Everything else will need to be replaced with an empty string ''. So, I know I need preg_replace to do this with, but Which RegEx is better to use, and why?

Thanks many guys!


The ^ inside your regex means NOT...and that is

[^a-zA-Z0-9]

means the string have not to have a-z, A-Z and 0-9 so if you want to replace all the chars which are not in those ranges (include the '_'), you have to use this statement:

$cleanString = preg_replace('/[^a-zA-Z0-9_]/', '', $theString);


The first character of the PCRE pattern string is a delimiter used to mark the end of the regular expression and the start of the modifier characters. The choice is arbitrary; you can use '/' or '~' or another character, but note that if you need the character in the expression part, then you will need to escape it.

In a character class, \s means any space character. Thus '/[^a-zA-Z0-9\\s]/' matches one character not in the set A-Z, a-z, 0-9, and space characters. '~[^A-Za-z0-9_]~' matches one character not in the set A-Z, a-z, 0-9, and underscore ('_').

One pattern string that meets your requirements is '~[^A-Za-z0-9_]+~s':

<?php
$str = <<<STR
test_

one

two Three  45
STR;

echo preg_replace('~[^A-Za-z0-9_]+~s', '', $str);

which outputs:

test_onetwoThree45

http://codepad.org/Ycl1WvR8

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜