1

我一直在尝试下一个正则表达式

$pattern = '/([0-9]{1,2}[.]{1}([a-z|A-Z|0-9]+[\s]*)+)/';

为了检测文本中出现的以下情况:[number].[text],例如:1.text text text 对于下一个文本"test number one 1.first 2.second",结果是

array(3) { [0]=> string(9) "1.first 2" [1]=> string(9) "1.first 2" [2]=> string(1) "2" }

我写的正则表达式的问题在哪里?

提前致谢 !

4

3 回答 3

0

这是你想要做的:

\d+[.][\w\d\s]+?(?=$|\s\d+[.])

它将匹配此文本中的所有 3 次出现:"1. text test 2. some more22 3. and more"

于 2013-03-13T20:58:46.363 回答
0

Try this:

$pattern = '/\d\.\w+/';
$str = "test number one 1.first 2.second 3.third 4.xyz 5.abcd";

preg_match_all($pattern, $str, $m);

print_r( $m );

The output is:

Array
(
    [0] => Array
    (
        [0] => 1.first
        [1] => 2.second
        [2] => 3.third
        [3] => 4.xyz
        [4] => 5.abcd
    )

)

is that what you need ? I'm not sure that I get well what you need to match so please try to explain it a bit more

You can say for example:

I would like to match x numbers followed by . followed by n letters

or something like that

于 2013-03-13T20:44:26.377 回答
0

你的问题是最后的加号,这意味着你可以在空格之后匹配东西。它应该是:

$pattern = '/([0-9]{1,2}[.]{1}([a-z|A-Z|0-9]+[\s]*))/';
于 2013-03-13T20:38:09.140 回答