正则表达式匹配任何空格 [英] regex match any whitespace

查看:662
本文介绍了正则表达式匹配任何空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用正则表达式和preg_replace函数进行替换.这是我的代码

I want to make a replacement using regex and preg_replace function. this is my code

$verif = "/wordA(\s*)wordB(?! wordc)/i";
$replacement = 'wordA wordb wordc';
$newvar = preg_replace($verif, $replacement, $article->text);

如果在wordA和wordB之间只有一个空格,这将起作用. 我需要匹配wordA和amp;之间的空格数量wordB.

That works if only we have one whitespace between wordA and wordB. I need to match what ever the number of whitespaces between wordA & wordB.

示例:

wordA(10个或更多的空格)wordB-> wordA wordb wordc相同的wordA(1空格)wordB-> wordA wordb wordc ...

wordA (10 or more whitespace) wordB -> wordA wordb wordc same wordA(1 whitespace) wordB -> wordA wordb wordc ...

推荐答案

您的正则表达式应按原样"运行.假设它正在执行您想要的操作.

Your regex should work 'as-is'. Assuming that it is doing what you want it to.

wordA(\s*)wordB(?! wordc)

这表示匹配wordA,后跟0或多个空格,后跟wordB,但不匹配,如果后跟wordc.请注意?!wordc之间的单个空格,这意味着wordA wordB wordc将不匹配,但是wordA wordB wordc将匹配.

This means match wordA followed by 0 or more spaces followed by wordB, but do not match if followed by wordc. Note the single space between ?! and wordc which means that wordA wordB wordc will not match, but wordA wordB wordc will.

以下是一些示例匹配项以及相关的替换输出:

Here are some example matches and the associated replacement output:

请注意,无论有多少空格,所有匹配项都将被替换.还有其他几点:-

Note that all matches are replaced no matter how many spaces. There are a couple of other points: -

  • (?! wordc)是一个负的超前查询,因此您不会匹配wordA wordB wordc行,这是假定的目的(这就是为什么最后一行不匹配的原因).当前,您要依靠?!之后的空格来匹配空白.您可能想更精确一些,并使用(?!\swordc).如果要在wordc之前与多个空格匹配,则可以根据需要使用(?!\s*wordc)表示0个或多个空格,或者(?!\s*+wordc)表示1个或多个空格. 当然,如果您确实希望在wordB之后用wordc匹配行,那么就不应该使用负的超前查询.

  • (?! wordc) is a negative lookahead, so you wont match lines wordA wordB wordc which is assume is intended (and is why the last line is not matched). Currently you are relying on the space after ?! to match the whitespace. You may want to be more precise and use (?!\swordc). If you want to match against more than one space before wordc you can use (?!\s*wordc) for 0 or more spaces or (?!\s*+wordc) for 1 or more spaces depending on what your intention is. Of course, if you do want to match lines with wordc after wordB then you shouldn't use a negative lookahead.

*将匹配0个或更多空格,因此将匹配wordAwordB.如果需要至少一个空格,则可能需要考虑+.

* will match 0 or more spaces so it will match wordAwordB. You may want to consider + if you want at least one space.

(\s*)-方括号表示捕获组.您是否出于某种原因将空白捕获到组中?如果没有,您可以只取下括号,即使用\s.

(\s*) - the brackets indicate a capturing group. Are you capturing the whitespace to a group for a reason? If not you could just remove the brackets, i.e. just use \s.

根据评论进行更新

您好,问题不在于表达式,而在于HTML出来的 不被认为是空格.这是一个Joomla网站.

Hello the problem is not the expression but the HTML out put   that are not considered as whitespace. it's a Joomla website.

保留原始正则表达式,可以使用:

Preserving your original regex you can use:

wordA((?:\s| )*)wordB(?!(?:\s| )wordc)

唯一的区别是正则表达式不匹配空格或 .我将wordc替换为\swordc,因为这更为明确.请注意,正如我已经指出的那样,当wordB后跟单个空格和wordc时,负前瞻?!匹配.如果要匹配多个空格,请参阅上面的我的评论.我也将捕获组保留在空白处,如果您不希望这样做,请按照上面已经描述的那样除去括号.

The only difference is that not the regex matches whitespace OR  . I replaced wordc with \swordc since that is more explicit. Note as I have already pointed out that the negative lookahead ?! will not match when wordB is followed by a single whitespace and wordc. If you want to match multiple whitespaces then see my comments above. I also preserved the capture group around the whitespace, if you don't want this then remove the brackets as already described above.

示例匹配项:

这篇关于正则表达式匹配任何空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆