如果检测到一个或多个空格字符,则正则表达式不匹配

时间:2013-03-28 09:36:17

标签: php regex preg-match-all

我的用户输入链接结构:

++visible part of link====invisible HTML address part of link++

输入字符串:

some text here some text here ++stack overflow====http://stackoverflow.com/questions/ask++ some text here ++examplesite.com====http://www.examplesite.com/article?id=1++ some text here some text here some text here some text here ++shouldnotmatch.com====http://w ww.shouldnotmatch.com/++ some text here.

我的目标:

如果====++之间的部分包含一个或多个空格字符,则preg_match_all不应匹配。所以我想要的输出是与前两次链接尝试相匹配。但是最后一次链接尝试不应该匹配,因为w ww包含一个空格字符。

我未成功的尝试:

  1. \+\+(.+?)====(.+?[^ ])\+\+
  2. \+\+(.+?)====(.+?[^ {1, }])\+\+
  3. 你能纠正我吗?

2 个答案:

答案 0 :(得分:1)

首次尝试时,您需要在空格验证之前允许所有字符。

这样的事情有用吗?

!\+\+(.+?)====([^ ]+?)\+\+!

如果这些括号之间总有一些东西,那么你可以放弃?

!\+\+(.+?)====([^ ]+)\+\+!

答案 1 :(得分:1)

试试这个正则表达式:

[+]{2}(.+?)[=]{4}([^\s]+?)[+]{2}