Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php RegExp匹配应答器[],内有A-Z 0-9_Php_Regex - Fatal编程技术网

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 ???
  • [A-Z]
    :1个字母,
    A-Z
  • [A-Z0-9\*
    :0或更多,
    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] )