Php RegExp匹配应答器[],内有A-Z 0-9
假设我有这样的东西:Php RegExp匹配应答器[],内有A-Z 0-9,php,regex,Php,Regex,假设我有这样的东西: $content = " some text [TAG_123] and other text"; 我想匹配[TAG\u 123] 更准确地说:[后跟一个大写字母A-Z,后跟零个或多个0-9A-Z,后跟] 我试过: $reg = "/\[[A-Z]+[0-9A-Z_]*/"; // => this match [TAG_123 $reg = "/\[[A-Z]+[0-9A-Z_]*\]/"; // => this doesn't work ??? [A-
$content = " some text [TAG_123] and other text";
我想匹配[TAG\u 123]
更准确地说:[
后跟一个大写字母A-Z
,后跟零个或多个0-9A-Z
,后跟]
我试过:
$reg = "/\[[A-Z]+[0-9A-Z_]*/"; // => this match [TAG_123
$reg = "/\[[A-Z]+[0-9A-Z_]*\]/"; // => this doesn't work ???
:1个字母,[A-Z]
A-Z
:0或更多,[A-Z0-9\*
,A-Z
或0-9
和\[
:字面上匹配\]
和[
]
$content=“一些文本[TAG_123]和其他文本”;
if(预匹配('/\[[A-Z][0-9A-Z]*\]/',$content,$matches)){
打印($matches);//$matches[0]包括[TAG_123]
}
您忘记在正则表达式中包含下划线:
$reg = "/\[[A-Z]+[0-9A-Z]*/"; // => this matches [TAG and not [TAG_123
您还需要从[A-Z]
中删除+
,因为它只需要一次
<?php
$content = " some text [TAG_123] and other text";
$regs="/\[[A-Z][0-9A-Z]*/";
preg_match($regs, $content, $matches);
print_r($matches);
$regs="/\[[A-Z][0-9A-Z_]*/";
preg_match($regs, $content, $matches);
print_r($matches);
$regs="/\[[A-Z][0-9A-Z_]*\]/";
preg_match($regs, $content, $matches);
print_r($matches);
您忘记了下划线
[0-9A-Z_]
-第一个匹配是[TAG not[TAG_123是的,忘记了“u”I已更新,但问题是sameOne大写字母A-Z?您已经有三个。第一个在[必须是强制的A-Z]之后,后面是零个或多个A-Z0-9_注意:如果您试图匹配TAG_123,则需要指定一个I
“/regexp/i”或使用a-zA-Z
Array ( [0] => [TAG )
Array ( [0] => [TAG_123 )
Array ( [0] => [TAG_123] )