簡體   English   中英

preg_match給出編譯失敗錯誤

[英]preg_match gives compilation failed error

我是一個總的正則表達式菜鳥,但是我有一個問題需要解決。

我有一個包含以下內容的文本文件...

String 1
Some variable
String 2

我看了一下,以為自己是這個的贏家,但我遇到了一個錯誤

preg_match( '/(String 1)(?:[^])*(String 2)/', $string, $matches );

但是我得到這個錯誤

preg_match():編譯失敗:字符類在偏移量33處缺少終止符]

有人可以幫我解決嗎?

謝謝

我相信您認為(?:[^])*與任何字符“零個或多個”匹配。

在Javascript中, [^]表示與任何字符匹配的有效字符類。 這對於PCRE來說是不同的,它會編譯為不完整的字符類。 使用點. 而是與s (dotall)修飾符結合使用。

preg_match('/(String 1).*?(String 2)/s', $string, $matches);

也許簡化並使其公正;

/^(String 1|String 2)$/m

例如;

<?php

$s = <<<STR
String 1
Some variable
String 2
STR;

$output_array = array();
preg_match("/^(String 1|String 2)$/m", $s, $output_array);

echo print_r($output_array, true);

實時預覽

  • ^ -匹配行/字符串的開頭
  • (..) -我們的捕獲小組
  • $ -匹配行/字符串的結尾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM