简体   繁体   English

php用正则表达式替换,并用正则表达式删除指定的模式

[英]php replace with regex and remove specified pattern with regex

|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Subject Name]|h|r

my usual printed out variable is ^ 我通常打印出来的变量是^

|cffffc700|Hitem:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x|h[SUBJECT_NAME]|h|r

my pattern is ^ 我的模式是^

ALL X's can be aZ, 0-9 所有X可以是aZ,0-9

in one column I have many variables like that (up to 8). 一栏中我有很多类似的变量(最多8个)。 and all variables are mixed with strings like that: 并且所有变量都与这样的字符串混合:

|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Gold]|h|r NEW SOLD |affffc451|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Copper]|h|r maximum price 15k|affffx312|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Silver]|h|r 

In one variable I want to clean all these unnecessary patterns and leave only subject name in brackets. 在一个变量中,我想清除所有这些不必要的模式,并仅在括号内保留主题名称。 [] []

So; 所以;

|cffffc700|Hitem:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x|h[SUBJECT NAME]|h|r

needs to leave only SUBJECT_NAME in my variable. 只需要在变量中保留SUBJECT_NAME。 just to remind, I have always more than one from these pattern in my every variable... (up to 8) I've searched it everywhere but couldn't find any reasonable answers NOR good patterns. 提醒一下,在每个变量中,我总是从这些模式中获取不止一个...(最多8个)我已经在所有位置进行了搜索,但找不到任何合理的答案,也没有好的模式。 Tried to make it myself but I guess I need to take all these patterns and make it array and clean it and only leave these subject names but I don't know exactly how to do it. 试图自己制作,但我想我需要采用所有这些模式,并将其排列并清理,仅保留这些主题名称,但我不知道该怎么做。

how do I convert this to : 我如何将其转换为:

|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Gold]|h|r NEW SOLD |affffc451|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Copper]|h|r maximum price 15k|affffx312|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Silver]|h|r 

this: 这个:

Gold NEW SOLD Copper maxiumum price 15k Silver

what should I use, preg_replace? 我应该使用preg_replace吗?


one more thing left, when I have a string without my special pattern, I get empty result from the function eg: 还有一件事,当我有一个没有特殊模式的字符串时,我从函数中得到空结果,例如:

$str = "15KKK sold, 20KK updated";

expected result: 预期结果:

"15KKK sold, 20KK updated" // same without any pattern

but ^ that one returns EMPTY result.. 但是^返回空结果。

another string: 另一个字符串:

$str = "|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Uranium]|h|r 155kk |affffc451|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Metal]|h|r is sold";

expected result: 预期结果:

"Uranium 155kk Metal is sold"

if I use that function with non-pattern string it returns empty result that's my problem now 如果我将该函数与非模式字符串一起使用,它将返回空结果,这就是我现在的问题

thank you very much 非常感谢你

try this regex: 试试这个正则表达式:

\|\w{9}\|Hitem(?::-?\w+)+\|h\[(?<SUBJECTNAME>\w+)\]\|h\|r

it will capture each variable sequence, as well as the relevant element name in the named group. 它将捕获每个变量序列以及命名组中的相关元素名称。

see the demo here 在这里查看演示

I'd do: 我会做:

$str = '|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Gold]|h|r NEW SOLD |affffc451|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Copper]|h|r maximum price 15k|affffx312|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Silver]|h|r';

preg_match_all('/h(\[.+?\])\|h\|r([^|]*)/', $str, $m);
for($i=0; $i<count($m[0]); $i++) {
    $res .= $m[1][$i] . ' '  . $m[2][$i] . ' ';
}
echo $res,"\n";

Output: 输出:

[Gold]  NEW SOLD  [Copper]  maximum price 15k [Silver]  

If you want to keep the strings that don't match, test the result of preg_match: 如果要保留不匹配的字符串,请测试preg_match的结果:

if (preg_match_all('/h(\[.+?\])\|h\|r([^|]*)/', $str, $m)) {
    for($i=0; $i<count($m[0]); $i++) {
        $res .= $m[1][$i] . ' '  . $m[2][$i] . ' ';
    }
} else {
    $res = $str;
}
echo $res,"\n";

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

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