0

我有 PCRE 模式:

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

和这段文字

$text ='来自 Millipore Milli-Q 系统(Billerica,MA,USA)的蒸馏水。
HPLC 级甲醇和乙腈购自 JT Baker Phillipsburg, NJ, USA)。
[标题]HEading 测试[/标题]
3-氨基荧蒽购自 Aldrich(密尔沃基,威斯康星州,美国)。
1-氨基芘
1-棕榈酰-甘油-3-磷酸胆碱
咖啡因
胞嘧啶
甲酸(LC/MS 级)
萘啶酸
茶碱
HPLC 级亮氨酸脑啡肽,购自 Fluka(密尔沃基,威斯康星州,美国)
阿米替林盐酸盐
β-Asp-Leu
异亮氨酸
γ- 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 级甲醇和乙腈购自 JT Baker Phillipsburg,NJ,USA)。[标题]标题测试[/标题]'

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

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

我想要的)

4

2 回答 2

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
                )

        )

)
于 2012-09-15T17:12:18.537 回答
-1

尝试用 (.|\n)* 替换最后一个 (.*)

于 2012-09-15T17:15:32.217 回答