Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 用于在单词后获取数字的正则表达式_Php_Regex - Fatal编程技术网

Php 用于在单词后获取数字的正则表达式

Php 用于在单词后获取数字的正则表达式,php,regex,Php,Regex,我想在我的错误消息中获取错误号。像 开头和结尾标记不匹配:en 第44行和货物 开始和结束标记不匹配: 描述第40行和分类 开始和结束标记不匹配: 分类信息第28行和卡片 标记类别中的数据过早结束 第27行 标签卡行中的数据过早结束 二, 我想搜索所有这些。为此,我需要一个正则表达式,比如:给我单词,并在单词行后面加上数字。它永远是一条直线。因为我从来没有和正则表达式一起工作过。我在读这本书,但到现在为止我还没有运气 我在php上这么做。请在这方面给我一些意见谢谢如果您只需要行号,请使用以下选项

我想在我的错误消息中获取错误号。像

开头和结尾标记不匹配:en 第44行和货物

开始和结束标记不匹配: 描述第40行和分类

开始和结束标记不匹配: 分类信息第28行和卡片

标记类别中的数据过早结束 第27行

标签卡行中的数据过早结束 二,

我想搜索所有这些。为此,我需要一个正则表达式,比如:给我单词,并在单词行后面加上数字。它永远是一条直线。因为我从来没有和正则表达式一起工作过。我在读这本书,但到现在为止我还没有运气


我在php上这么做。请在这方面给我一些意见谢谢

如果您只需要行号,请使用以下选项:

$msg = 'Opening and ending tag mismatch: en line 44 and goods';

if (preg_match('#\bline (\d+)#', $msg, $matches)) {
    echo "line is: " . $matches[0] . "\n";
}
如果要同时匹配所有行号:

$msgs = <<<EOF
If you want to match all lines in all messages at once:

Opening and ending tag mismatch: en line 44 and goods

Opening and ending tag mismatch: describtion line 40 and categorie

Opening and ending tag mismatch: categorieInfo line 28 and card

Premature end of data in tag categorie line 27

Premature end of data in tag card line 2
EOF;

preg_match_all('#^.*\bline (\d+).*$#m', $msgs, $matches, PREG_SET_ORDER);
foreach($matches as $msg) {
    echo "message: " . $msg[0] . "\n";
    echo "line: " . $msg[1] . "\n";
}

如果只需要行号,请使用以下命令:

$msg = 'Opening and ending tag mismatch: en line 44 and goods';

if (preg_match('#\bline (\d+)#', $msg, $matches)) {
    echo "line is: " . $matches[0] . "\n";
}
如果要同时匹配所有行号:

$msgs = <<<EOF
If you want to match all lines in all messages at once:

Opening and ending tag mismatch: en line 44 and goods

Opening and ending tag mismatch: describtion line 40 and categorie

Opening and ending tag mismatch: categorieInfo line 28 and card

Premature end of data in tag categorie line 27

Premature end of data in tag card line 2
EOF;

preg_match_all('#^.*\bline (\d+).*$#m', $msgs, $matches, PREG_SET_ORDER);
foreach($matches as $msg) {
    echo "message: " . $msg[0] . "\n";
    echo "line: " . $msg[1] . "\n";
}

向我们展示您迄今为止的成果。向我们展示您迄今为止的成果。\\b行\\d+会稍微好一点,并且不会匹配我不感兴趣的换行10中的10,我想要换行20。@xotix它是一种heredoc,一种书写长文字字符串的替代方法。好的,谢谢。我必须为“给你一些代表”注册。是否可以只获取号码而不使用电话线?我自己试试,或者干脆把它剪掉。谢谢:在第一个解决方案中,行是$matches[0],在第二个解决方案中,行是$msg[1],我的意思是,如果我有if preg_match'\bline\d+,$msg,$matches{return$matches[0];}我得到了第2行、第4行、第5行、第6行等等,但我只得到了第2、4、5、6行。我可以对正则表达式说,只是数字吗?\\bline\\d+会稍微好一点,不会匹配我不感兴趣的换行10中的10,我想要换行20。@xotix它是一个herdeoc,一种书写长文字字符串的替代方法,好的,谢谢。我必须为“给你一些代表”注册。是否可以只获取号码而不使用电话线?我自己试试,或者干脆把它剪掉。谢谢:在第一个解决方案中,行是$matches[0],在第二个解决方案中,行是$msg[1],我的意思是,如果我有if preg_match'\bline\d+,$msg,$matches{return$matches[0];}我得到了第2行、第4行、第5行、第6行等等,但我只得到了第2、4、5、6行。我能对正则表达式说,只是数字吗?