PHP Preg_match:尝试匹配字符串中不存在子字符串的字符串

发布于 2024-09-24 21:19:02 字数 605 浏览 3 评论 0原文

我一直在努力让这个正则表达式工作。假设解析一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

檐上三寸雪 2024-10-01 21:19:02

只需匹配 _skipThis 即可,如果找到则返回 false

 if (strpos($theString, '_skipThis') === false) {
        // perform preg_match
 } else 
        return false;

(当然有一个正则表达式。假设 _skipThis 只出现在第一个 / 之前,

 return preg_match('|^([^/]+)(?<!_skipThis)(/.*)$|', $theString); 
 //                   -------              -----
 //                     $1   --------------  $2
 //                          Make sure it is *not* preceded '_skipThis'

否则,如果 _skipThis 出现在任何地方都需要避免,

 return preg_match('|^(?!.*_skipThis)([^/]+)(/.*)$|', $theString);
 //                   ---------------
 //                   Make sure '_skipThis' is not found anywhere. 

Just match _skipThis and return false if it is found.

 if (strpos($theString, '_skipThis') === false) {
        // perform preg_match
 } else 
        return false;

(Of course there is a regex for this. Assuming _skipThis only appears before the first /,

 return preg_match('|^([^/]+)(?<!_skipThis)(/.*)$|', $theString); 
 //                   -------              -----
 //                     $1   --------------  $2
 //                          Make sure it is *not* preceded '_skipThis'

Otherwise, if the _skipThis appearing anywhere needs to be avoided,

 return preg_match('|^(?!.*_skipThis)([^/]+)(/.*)$|', $theString);
 //                   ---------------
 //                   Make sure '_skipThis' is not found anywhere. 
苹果你个爱泡泡 2024-10-01 21:19:02

试试这个:

$str1 = 'a_particular_part_of_string/a/b/c/d/e/f'; //1
$str2 = 'a_particular_part_of_string_skipThis/a/b/c/d/e/f'; //0
$keep = 'a_particular_part_of_string';
$skip = '_skipThis';

$m = array();
if(preg_match("/($keep)(?!$skip)(.*)$/", $str1, $m))
    var_dump($m);
else
    echo "Not found\n";

$m = array();
if(preg_match("/($keep)(?!$skip)(.*)$/", $str2, $m))
    var_dump($m);
else
    echo "Not found\n";

输出:

array(3) {
  [0]=>
  string(39) "a_particular_part_of_string/a/b/c/d/e/f"
  [1]=>
  string(27) "a_particular_part_of_string"
  [2]=>
  string(12) "/a/b/c/d/e/f"
}

Not found

Try this :

$str1 = 'a_particular_part_of_string/a/b/c/d/e/f'; //1
$str2 = 'a_particular_part_of_string_skipThis/a/b/c/d/e/f'; //0
$keep = 'a_particular_part_of_string';
$skip = '_skipThis';

$m = array();
if(preg_match("/($keep)(?!$skip)(.*)$/", $str1, $m))
    var_dump($m);
else
    echo "Not found\n";

$m = array();
if(preg_match("/($keep)(?!$skip)(.*)$/", $str2, $m))
    var_dump($m);
else
    echo "Not found\n";

Output:

array(3) {
  [0]=>
  string(39) "a_particular_part_of_string/a/b/c/d/e/f"
  [1]=>
  string(27) "a_particular_part_of_string"
  [2]=>
  string(12) "/a/b/c/d/e/f"
}

Not found
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文