preg_replace php 语法代码
preg_replace php syntax code
我需要将 ","
替换为 |,|
内部模式而不替换任何其他地方
我喜欢这个代码
[word:"pla pla","pla pla","[other_word:"pla pla","[word:"pla","pla"end word]","pla pla"end other_word]","pla pla","[word:"pla","pla"end word]"end word]
结果一定是这样的
[word:"pla pla|,|pla pla|,|[other_word:"pla pla","[word:"pla","pla"end word]","pla pla"end other_word]|,|pla pla|,|[word:"pla","pla"end word]"end word]
我当前的代码是:
preg_replace('/\[([\w]+):\"[^\",\"]*\"end\s\w\](.*?)\[([\w]+):\"\"end\s\w\]/', '|^|', $syn);
此模式旨在仅在第一级或方括号内替换 ","
:
$pattern = '~
# this part defines subpatterns to be used later in the main pattern
(?(DEFINE)
(?<nestedBrackets> \[ [^][]* (?:\g<nestedBrackets>[^][]*)*+ ] )
)
# the main pattern
(?: # two possible entry points
\G(?!\A) # 1. contiguous to a previous match
| # OR
[^[]* \[ # 2. all characters until an opening bracket
)
# all possible characters until "," or the closing bracket:
[^]["]* # all that is not ] [ or "
(?:
\g<nestedBrackets> [^]["]* # possible nested brackets
| # OR
"(?!,") [^]["]* # a quote not followed by ,"
)*+ # repeat as needed
\K # remove all on the left from match result
(?:
"," # match the target
|
] (*SKIP)(*F) # closing bracket: break the contiguity
)
~x';
$str = preg_replace($pattern, '|,|', $str);
我需要将 ","
替换为 |,|
内部模式而不替换任何其他地方
我喜欢这个代码
[word:"pla pla","pla pla","[other_word:"pla pla","[word:"pla","pla"end word]","pla pla"end other_word]","pla pla","[word:"pla","pla"end word]"end word]
结果一定是这样的
[word:"pla pla|,|pla pla|,|[other_word:"pla pla","[word:"pla","pla"end word]","pla pla"end other_word]|,|pla pla|,|[word:"pla","pla"end word]"end word]
我当前的代码是:
preg_replace('/\[([\w]+):\"[^\",\"]*\"end\s\w\](.*?)\[([\w]+):\"\"end\s\w\]/', '|^|', $syn);
此模式旨在仅在第一级或方括号内替换 ","
:
$pattern = '~
# this part defines subpatterns to be used later in the main pattern
(?(DEFINE)
(?<nestedBrackets> \[ [^][]* (?:\g<nestedBrackets>[^][]*)*+ ] )
)
# the main pattern
(?: # two possible entry points
\G(?!\A) # 1. contiguous to a previous match
| # OR
[^[]* \[ # 2. all characters until an opening bracket
)
# all possible characters until "," or the closing bracket:
[^]["]* # all that is not ] [ or "
(?:
\g<nestedBrackets> [^]["]* # possible nested brackets
| # OR
"(?!,") [^]["]* # a quote not followed by ,"
)*+ # repeat as needed
\K # remove all on the left from match result
(?:
"," # match the target
|
] (*SKIP)(*F) # closing bracket: break the contiguity
)
~x';
$str = preg_replace($pattern, '|,|', $str);