我正在尝试使用正则表达式匹配以下模式:
Something-23432
我正在使用以下代码来查找它使用正则表达式
preg_match('/^Something'-/[0-9/]+/', $msg, $matches);
它不起作用:/。任何帮助都将不胜感激!
在整个文件中,上述模式只会出现一次。
编辑
<?php
preg_match('/^Something'-[0-9]+$/', "I am looking for Something-2343. I am not sure if this script can find it.", $matches);
print_r($matches);
?>
此代码只生成
Array ( )
将您的行更改为:preg_match('/^Something'-[0-9]+$/', $msg, $matches);
<?php
$msg="Something-23432";
if( preg_match('/^Something'-[0-9]+$/', $msg, $matches)) {
echo 'found';
}
else {
echo "Not found";
}
print_r($matches);
?>
输出:
found Array ( [0] => Something-23432 )
preg_match('/Something'-[0-9]+/', $msg, $matches);
如果你特别想要五个字符,
preg_match('/Something'-[0-9]{5}/', $msg, $matches);