简体   繁体   English

不匹配括号中的字符串php正则表达式

[英]don't match string in brackets php regex

I've been trying to use preg_replace() in php to replace string.我一直在尝试在 php 中使用 preg_replace() 来替换字符串。 I want to match and replace all 's' in this string, but I just came with solution only mathching 's' between 'b' and 'c' or 's' between > <.我想匹配并替换此字符串中的所有 's',但我只是提供了解决方案,仅在 'b' 和 'c' 之间使用了 's' 或 > < 之间的 's'。 Is there any way I can use negative look behind not just for the character '>' but for whole string ?有什么方法可以让我不仅对字符 '>' 而是对整个字符串使用负向外观吗? I don't want to replace anything in brackets.我不想替换括号中的任何内容。

     <text size:3>s<text size:3>absc
     <text size:3>xxetxx<text size:3>sometehing

edit: just get 's' in >s< and in bsc.编辑:只需在 >s< 和 bsc 中获取 's'。 Then when I will change string for example from 's' to 'te', to replace 'te' in xtex and sometehing.然后,当我将字符串例如从“s”更改为“te”时,以替换 xtex 中的“te”和一些东西。 So I was looking for regular expression to avoid replacing anything in <....>所以我正在寻找正则表达式以避免替换 <....> 中的任何内容

You can use this pattern:您可以使用此模式:

$pattern = '/((<[^>]*>)*)([^s]*)s/';
$replace = '\1\3■';                                      # ■ = your replacement string

$result = preg_replace( $pattern, $replace, $str );

regex101 demo regex101 演示

Pattern explanation:图案说明:

(               # group 1:
(<[^>]*>)*      # group 2: zero-or-more <...>
)    
([^s]*)         # group 3: zero-or-more not “s”
s               # litterally “s”

If you want match case-insensitive, add a “i” at the end of pattern:如果您希望匹配不区分大小写,请在模式末尾添加“i”:

$pattern = '/((<[^>]*>)*)([^s]*)s/i';

Edit: Replacement explanation编辑:替换说明

In the search pattern we have 3 groups surrounded by round brackets.在搜索模式中,我们有 3 个用圆括号括起来的组。 In the replace string we can refer to groups by syntax \\1 , where 1 is the group number.在替换字符串中,我们可以通过语法\\1来引用组,其中1是组号。

So, replace string in the example means: replace group 1 with itself, replace group 3 with itself, replace “s” with desired replacement.因此,示例中的替换字符串表示:将组 1 替换为自身,将组 3 替换为自身,将“s”替换为所需的替换。 We don't need to use group 2 because it is included in group 1 (this due to regex impossibility to retrieve repeating groups).我们不需要使用组 2,因为它包含在组 1 中(这是由于正则表达式无法检索重复组)。

In the demo string:在演示字符串中:

abs<text size:3>ssss<text size:3><img src="img"><text size:3>absc
└┘╵└───────────┘╵╵╵╵└───────────────────────────────────────┘└┘╵╵
└─┘└────────────┘╵╵╵└──────────────────────────────────────────┘
 1  2            345 6

Pattern matches:模式匹配:

     group 1    group 3       s
    ---------  ---------  ---------
1 >     0          1          1
2 >     1          0          1
3 >     0          0          1
4 >     0          0          1
5 >     0          0          1    
6 >     3          1          1

The last “c” is not matches, so is not replaced.最后一个“c”不匹配,因此不会被替换。

Use preg_match_all to get all the s letters and use it with flag PREG_OFFSET_CAPTURE to get the indices.使用preg_match_all获取所有s字母并将其与标志PREG_OFFSET_CAPTURE一起使用以获取索引。

The regular expression $pat contains a negative lookahead and lookbehind so that the s inside the brackets expression is not matched.正则表达式$pat包含否定的前瞻和后视,因此括号表达式中的s不匹配。

In this example I replace s with the string 5 .在本例中,我将s替换为字符串5 Change to the string you want to substitute:更改为要替换的字符串:

<?php

$s = " <text size:3>s<text size:3>absc";
$pat = "/(?<!\<text )s(?!ize:3\>)/";

preg_match_all($pat, $s, $matches, PREG_OFFSET_CAPTURE);

foreach ($matches[0] as $match) {
    $s[$match[1]] = "5";
}

print_r(htmlspecialchars($s));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM