PCRE,正则表达式没有检测子模式

时间:2012-09-15 17:06:34

标签: php regex pcre

我有PCRE模式:

$pattern = '/(.*)(\[(heading)\](.*)\[\/\3\])(.*)/Us'

和本文

$ text ='Millipore Milli-Q系统(美国马萨诸塞州比尔里卡)的蒸馏水。
HPLC级甲醇和乙腈购自J.T.Baker Phillipsburg,NJ,USA) [标题]标题测试[/标题]
3-氨基荧蒽购自Aldrich(Milwaukee,WI,USA) 1-氨基芘
1-棕榈酰 - 甘油-3-磷酸胆碱
咖啡因
胞嘧啶
甲酸(LC / MS级)
萘啶酸
茶碱
HPLC级亮氨酸脑啡肽购自Fluka(Milwaukee,WI,USA)
阿米替林盐酸盐
β-ASP-亮氨酸
异亮氨酸
γ-GLU-LEU
马尿酸
亮氨酸
1-油酰基甘油基-3-磷酸胆碱
苯丙氨酸
1-硬脂酰甘油 - 磷酸胆碱
色氨酸和酪氨酸购自Sigma(St.Louis,MO,USA)。
'

当我跑步时:

preg_match_all($pattern,$text,$matches,PREG_OFFSET_CAPTURE)

它没有捕获最后一个子模式,最后是'(。*)',我不明白为什么不。有什么解释吗?

(也就是说,它捕获了来自Millipore Milli-Q系统(Billerica,MA,USA)的蒸馏水。 HPLC级甲醇和乙腈购自J.T.Baker Phillipsburg,NJ,USA)。 [标题]标题测试[/标题]'

但不是字符串的最后一部分:

” 3-氨基荧蒽购自Aldrich(Milwaukee,WI,USA)。 1-氨基芘 1-棕榈酰 - 甘油-3-磷酸胆碱 咖啡因 胞嘧啶 甲酸(LC / MS级) 萘啶酸 茶碱 HPLC级亮氨酸脑啡肽购自Fluka(Milwaukee,WI,USA) 阿米替林盐酸盐 β-ASP-亮氨酸 异亮氨酸 γ-GLU-LEU 马尿酸 亮氨酸 1-油酰基甘油基-3-磷酸胆碱 苯丙氨酸 1-硬脂酰甘油 - 磷酸胆碱 色氨酸和酪氨酸购自Sigma(St.Louis,MO,USA)。'

我想要的)

2 个答案:

答案 0 :(得分:1)

当我删除贪婪反转标记时,它对我来说很好。

$pattern = '/(.*)(\[(heading)\](.*)\[\/\3\])(.*)/s';

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Distilled water from a Millipore Milli-Q system (Billerica, MA, USA).
HPLC-grade methanol and acetonitrile purchased from J. T. Baker Phillipsburg, NJ, USA).
[heading]HEading test[/heading]
3-Aminofluoranthene purchased from Aldrich (Milwaukee, WI, USA).
1-Aminopyrene
1-palmitoyl-glycero-3-phosphocholine
caffeine
cytosine
formic acid (LC/MS grade)
nalidixic acid
theophylline
HPLC-grade leucine enkephalin purchased from Fluka (Milwaukee, WI, USA)
Amitriptyline hydrochloride
β-Asp-Leu
isoleucine
γ-Glu-Leu
hippuric acid
leucine
1-oleoyl-glycero-3-phosphocholine
phenylalanine
1-stearoyl-glycero-phosphocholine
tryptophan and tyrosine purchased from Sigma (St. Louis, MO, USA).

                    [1] => 0
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => Distilled water from a Millipore Milli-Q system (Billerica, MA, USA).
HPLC-grade methanol and acetonitrile purchased from J. T. Baker Phillipsburg, NJ, USA).

                    [1] => 0
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => [heading]HEading test[/heading]
                    [1] => 160
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [0] => heading
                    [1] => 161
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [0] => HEading test
                    [1] => 169
                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [0] => 
3-Aminofluoranthene purchased from Aldrich (Milwaukee, WI, USA).
1-Aminopyrene
1-palmitoyl-glycero-3-phosphocholine
caffeine
cytosine
formic acid (LC/MS grade)
nalidixic acid
theophylline
HPLC-grade leucine enkephalin purchased from Fluka (Milwaukee, WI, USA)
Amitriptyline hydrochloride
β-Asp-Leu
isoleucine
γ-Glu-Leu
hippuric acid
leucine
1-oleoyl-glycero-3-phosphocholine
phenylalanine
1-stearoyl-glycero-phosphocholine
tryptophan and tyrosine purchased from Sigma (St. Louis, MO, USA).

                    [1] => 191
                )

        )

)

答案 1 :(得分:-1)

尝试用(。| \ n)*

替换最后一个(。*)