PHP Preg_match:尝试匹配字符串中不存在子字符串的字符串
我一直在努力让这个正则表达式工作。假设解析一个 URL,如果找到字符串 '_skipThis',则不匹配该字符串。还需要反向引用。例如:
String 1: a_particular_part_of_string/a/b/c/d/e/f
Result: preg_match should return true
Backreference: $1 -> a_particular_part_of_string, $2 -> /a/b/c/d/e/f
String 2: a_particular_part_of_string_skipThis/a/b/c/d/e/f
Result: preg_match should return false.
Backreference: nothing here.
我尝试了以下正则表达式..
reg1 = ([a-zA-Z0-9_]+)(\/.*)
reg2 = ([a-zA-Z0-9]+(?!_skipThis))(\/.*)
reg3 = ((?!_skipThis).*)(\/.*)
reg4 = ((?!_skipThis)[a-zA-Z0-9_]+)(\/.*)
请帮助我!提前致谢!!!
I have been trying to get this regex work. Suppose to parse an URL, if the string '_skipThis' is found, don't match the string. Also backreferences are needed too. For example:
String 1: a_particular_part_of_string/a/b/c/d/e/f
Result: preg_match should return true
Backreference: $1 -> a_particular_part_of_string, $2 -> /a/b/c/d/e/f
String 2: a_particular_part_of_string_skipThis/a/b/c/d/e/f
Result: preg_match should return false.
Backreference: nothing here.
I have tried the following regex..
reg1 = ([a-zA-Z0-9_]+)(\/.*)
reg2 = ([a-zA-Z0-9]+(?!_skipThis))(\/.*)
reg3 = ((?!_skipThis).*)(\/.*)
reg4 = ((?!_skipThis)[a-zA-Z0-9_]+)(\/.*)
Please help me! Thanks in advance!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需匹配
_skipThis
即可,如果找到则返回 false。(当然有一个正则表达式。假设
_skipThis
只出现在第一个/
之前,否则,如果
_skipThis
出现在任何地方都需要避免,Just match
_skipThis
and return false if it is found.(Of course there is a regex for this. Assuming
_skipThis
only appears before the first/
,Otherwise, if the
_skipThis
appearing anywhere needs to be avoided,试试这个:
输出:
Try this :
Output: