Php 正则表达式,除非在“内部”;“语音标记”;
如果我想符合以下条件:Php 正则表达式,除非在“内部”;“语音标记”;,php,regex,string,preg-replace,Php,Regex,String,Preg Replace,如果我想符合以下条件: slashdot <-Hit " slashdot " <-Hit " slashdot <-Hit slashdot " <-Hit "slashdot" <-Miss 但是我在设置它们时遇到了困难,因此如果有任何空格,仍然会命中 非常感谢大家的帮助。(我是stackoverflow的新手,非常喜欢它!我正在尽可
slashdot <-Hit
" slashdot " <-Hit
" slashdot <-Hit
slashdot " <-Hit
"slashdot" <-Miss
但是我在设置它们时遇到了困难,因此如果有任何空格,仍然会命中
非常感谢大家的帮助。(我是stackoverflow的新手,非常喜欢它!我正在尽可能多地回答别人的问题,并学习我所听到的一切)
简言之
slashdot - good
"slashdot" - bad
" slashdot " - good (as there are spaces)
描述
这个正则表达式将只查找没有引号的行,或者引号和它的负载之间有空格的行。此表达式假定第一个非空格字符是引号(如果存在)。如果第一个非空格字符不是开引号,则允许整行
^\s*(?:[^”].*
PHP代码示例:
输入字符串
slashdot
" slashdot "
" multiline 1 slashdot
line 2 slashdot "
"slashdot bad"
" leading spaces and trailing spaces "
代码
<?php
$sourcestring="your source string";
preg_match_all('/^(?:[^"].*|"\s[^"]*\s")/imx',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
您好-您是否正在查找“
和”
之间的所有内容,其中前面有空格,后面有引号?我希望所有内容都与“slashdot”匹配,除非它在没有空格的speechmarks内。sso slashdot-good“slashdot”-bad“slashdot”-good(因为有空格)我可以问一下您是否只想去掉引号吗?因为坏的部分或不匹配的部分实际上不是regex的函数。我可以编写一个返回slashdot的表达式,但您可能需要检查“slashdot”“
previor。如果你能提供一些你想要达到的目标,那就太好了。哇!谢谢你的帮助。非常感谢。我希望我也能很快为其他人回答很多问题,这对stackoverflow来说是个新鲜事,但这是一个多么棒的社区啊!:)+1为可怕的图表。你有产生这种效果的工具吗?@Racheet,我在使用debuggex.com。尽管它不支持lookbehinds、命名捕获组或原子组,但它仍然便于理解表达式流。还有regexper.com。他们也做得很好,但在你打字时不是实时的。
<?php
$sourcestring="your source string";
preg_match_all('/^(?:[^"].*|"\s[^"]*\s")/imx',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
$matches Array:
(
[0] => Array
(
[0] => slashdot
[1] => " slashdot "
[2] => " multiline 1 slashdot
[3] => line 2 slashdot "
[4] => " leading spaces and trailing spaces "
)
)