“[reply]”和提取回复编号之间的所有内容
Everything between "[reply]" and extract the reply number
请看下面的情况
[reply="292"] Text Here [/reply]
我想得到的是reply="NUMBERS"
中引号之间的数字。我想将其提取到一个变量,并将 [reply="NUMBER"] this text here [/reply]
之间的文本提取到另一个变量。
所以对于这个例子:
[reply="292"] Text Here [/reply]
我要提取回复编号:292
和 reply
标签之间的文本:Text here
.
我试过这个:
\[reply\=\"]([A-Z]\w)\[\/reply]
但这只在 reply
标签之前有效,之后就无效了。我该怎么做呢?
简单!
\[reply\=\"(\d+)\"](.*?)\[\/reply]
说明
\d
数字
+
指定字符出现 1 次或多次。
[\w\s]
对于单词和空格中的任何字符 (\s
)
然后像这样将其应用于 PHP:
<?php
$str = "[reply=\"292\"] Text Here [/reply]";
preg_match('/\[reply\=\"(\d+)\"]([\w\s]+)\[\/reply]/', $str, $re);
print_r($re[1]); // printing group 1, the reply number
print_r($re[2]); // printing group 2, the text
?>
重要!!
只获取组值,不是全部。反正你只需要其中的一部分。
我保留了泛型 (.*),但您可以指定一个类型,例如小数 (\d+)。
php:
$s = '[reply="292"] Text Here [/reply]';
$expr = '/\[reply=\"(.*)\"\](.*)\[\/reply\]/';
if(preg_match($expr,$s,$r)){
var_dump($r);
}
javascript:
s = '[reply="292"] Text Here [/reply]'
s.match(/\[reply=\"(.*)\"\](.*)\[\/reply\]/)
//["[reply="292"] Text Here [/reply]", "292", " Text Here "]
请看下面的情况
[reply="292"] Text Here [/reply]
我想得到的是reply="NUMBERS"
中引号之间的数字。我想将其提取到一个变量,并将 [reply="NUMBER"] this text here [/reply]
之间的文本提取到另一个变量。
所以对于这个例子:
[reply="292"] Text Here [/reply]
我要提取回复编号:292
和 reply
标签之间的文本:Text here
.
我试过这个:
\[reply\=\"]([A-Z]\w)\[\/reply]
但这只在 reply
标签之前有效,之后就无效了。我该怎么做呢?
简单!
\[reply\=\"(\d+)\"](.*?)\[\/reply]
说明
\d
数字+
指定字符出现 1 次或多次。[\w\s]
对于单词和空格中的任何字符 (\s
)
然后像这样将其应用于 PHP:
<?php
$str = "[reply=\"292\"] Text Here [/reply]";
preg_match('/\[reply\=\"(\d+)\"]([\w\s]+)\[\/reply]/', $str, $re);
print_r($re[1]); // printing group 1, the reply number
print_r($re[2]); // printing group 2, the text
?>
重要!!
只获取组值,不是全部。反正你只需要其中的一部分。
我保留了泛型 (.*),但您可以指定一个类型,例如小数 (\d+)。
php:
$s = '[reply="292"] Text Here [/reply]';
$expr = '/\[reply=\"(.*)\"\](.*)\[\/reply\]/';
if(preg_match($expr,$s,$r)){
var_dump($r);
}
javascript:
s = '[reply="292"] Text Here [/reply]'
s.match(/\[reply=\"(.*)\"\](.*)\[\/reply\]/)
//["[reply="292"] Text Here [/reply]", "292", " Text Here "]