php 匹配正则表达式中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/559363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matching a space in regex
提问by Gavin Schulz
I need to match a space character in a PHP regular expression. Anyone got any ideas?
我需要在 PHP 正则表达式中匹配一个空格字符。有人有任何想法吗?
I mean like "gavin schulz", the space in between the two words. I am using a regular expression to make sure that I only allow letters, number and a space. But I'm not sure how to find the space. This is what I have right now:
我的意思是“gavin schulz”,这两个词之间的空格。我正在使用正则表达式来确保我只允许使用字母、数字和空格。但我不确定如何找到空间。这就是我现在所拥有的:
$newtag = preg_replace("/[^a-zA-Z0-9s|]/", "", $tag);
回答by paxdiablo
If you're looking for a space, that would be " "(one space).
如果您正在寻找一个空间,那就是" "(一个空间)。
If you're looking for one or more, it's " *"(that's twospaces and an asterisk) or " +"(one space and a plus).
如果您正在寻找一个或多个,它是" *"(即两个空格和一个星号)或" +"(一个空格和一个加号)。
If you're looking for common spacing, use "[ X]"or "[ X][ X]*"or "[ X]+"where Xis the physical tab character (and each is preceded by a single space in all those examples).
如果您正在寻找通用间距,请使用"[ X]"or"[ X][ X]*"或"[ X]+"whereX是物理制表符(并且在所有这些示例中,每个空格前面都有一个空格)。
These will work in every* regex engine I've ever seen (some of which don't even have the one-or-more "+"character, ugh).
这些将适用于我见过的每个* 正则表达式引擎(其中一些甚至没有一个或多个"+"字符,呃)。
If you know you'll be using one of the more modern regex engines, "\s"and its variations are the way to go. In addition, I believe word boundaries match start and end of lines as well, important when you're looking for words that may appear without preceding or following spaces.
如果您知道您将使用一种更现代的正则表达式引擎,那么"\s"它的变体就是您要走的路。此外,我相信单词边界也匹配行的开头和结尾,这在您查找可能出现前或后没有空格的单词时很重要。
For PHP specifically, this pagemay help.
特别是对于 PHP,此页面可能会有所帮助。
From your edit, it appears you want to remove all non valid characters The start of this is (note the space inside the regex):
从您的编辑中,您似乎想删除所有无效字符 开头是(注意正则表达式中的空格):
$newtag = preg_replace ("/[^a-zA-Z0-9 ]/", "", $tag);
# ^ space here
If you also want trickery to ensure there's only one space between each word and none at the start or end, that's a little more complicated (and probably another question) but the basic idea would be:
如果您还想通过技巧确保每个单词之间只有一个空格,而在开头或结尾没有空格,那会稍微复杂一些(可能还有另一个问题),但基本思想是:
$newtag = preg_replace ("/ +/", " ", $tag); # convert all multispaces to space
$newtag = preg_replace ("/^ /", "", $tag); # remove space from start
$newtag = preg_replace ("/ $/", "", $tag); # and end
回答by davethegr8
回答by Fletcher Ripp
Here is a everything you need to know about whitespace in regular expressions:
以下是您需要了解的有关正则表达式中空格的所有信息:
[[:blank:]]Space or tab only[[:space:]]Whitespace\sAny whitespace character\vVertical whitespace\hHorizontal whitespacexIgnore whitespace
[[:blank:]]仅空格或制表符[[:space:]]空白\s任何空白字符\v垂直空白\h水平空白x忽略空格
回答by Kibbee
It seems to me like using a REGEX in this case would just be overkill. Why not just just strposto find the space character. Also, there's nothing special about the space character in regular expressions, you should be able to search for it the same as you would search for any other character. That is, unless you disabled pattern whitespace, which would hardly be necessary in this case.
在我看来,在这种情况下使用 REGEX 简直是矫枉过正。为什么不只是strpos来查找空格字符。此外,正则表达式中的空格字符没有什么特别之处,您应该能够像搜索任何其他字符一样搜索它。也就是说,除非您禁用模式空白,否则在这种情况下几乎不需要。
回答by Elsporko
In Perl the switch is \s(whitespace).
在 Perl 中,开关是\s(whitespace)。
回答by Peter Boughton
I am using a regex to make sure that I only allow letters, number and a space
我正在使用正则表达式来确保我只允许使用字母、数字和空格
Then it is as simple as adding a space to what you've already got:
然后就像在你已经拥有的东西上添加一个空格一样简单:
$newtag = preg_replace("/[^a-zA-Z0-9 ]/", "", $tag);
(note, I removed the s|which seemed unintentional? Certainly the swas redundant; you can restore the |if you need it)
(注意,我删除了s|似乎是无意的?当然s是多余的;|如果需要,您可以恢复)
If you specifically want *a* space, as in onlya single one, you will need a more complex expression than this, and might want to consider a separate non-regex piece of logic.
如果您特别想要 *a* 空间,因为只有一个空间,您将需要比这更复杂的表达式,并且可能需要考虑单独的非正则表达式逻辑。
回答by Suroot
You can also use the \b for a word boundary. For the name I would use something like this:
您还可以使用 \b 作为单词边界。对于名字,我会使用这样的东西:
[^\b]+\b[^\b]+(\b|$)
EDITModifying this to be a regex in Perl example
编辑将其修改为 Perl 示例中的正则表达式
if( $fullname =~ /([^\b]+)\b[^\b]+([^\b]+)(\b|$)/ ) {
$first_name = ;
$last_name = ;
}
EDIT AGAINBased on what you want:
再次编辑基于你想要的:
$new_tag = preg_replace("/[\s\t]/","",$tag);
回答by Jeremy Schultz
I'm trying out [[:space:]] in an instance where it looks like bloggers in WordPress are using non-standard space characters. It looks like it will work.
我正在尝试 [[:space:]] 在一个例子中,它看起来像 WordPress 中的博主正在使用非标准空格字符。看起来它会起作用。
回答by Ajay Singh
Use it like this to allow for single space.
像这样使用它以允许单个空间。
$newtag = preg_replace("/[^a-zA-Z0-9\s]/", "", $tag)

