0

所以,我试图从文本文件中捕获 3 个字母的单词。我创建了一个 RegEx,但它返回一个 EMPTY 数组。我不知道为什么!这是文本文件的一部分。

================================================
Header of File with time and date
================================================
Loaded options from XML file: '/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/ID2PDF_options.xml
extendedPrintPDF started
Postfix '3.0' was append from file 'ESQ030112ELAM_lo-metadata.xml' for file: '/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/someFile.indd
printPDF started
PDF Export Preset: Some preset
PDF file created: ''/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/someFile.pdf'.
File someFileName.xml removed
postprocessingDocument started
INDD file removed: '/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/someFile.indd

这是我的正则表达式:

/^Loaded options from XML file: '\/.*\/SM_Folder\/([a-zA-Z]{3})\/[a-zA-Z]+_Proof_\1\/processing\/ID2PDF_options.xml$/im

\如果我在 前面 删除\/([a-zA-Z]{3}),我会收到Unknown modifier:(错误消息。

有人可以告诉我我需要做什么才能从记录的第一行获取“ESQ”吗?3 个字母的单词在其他记录中会有所不同,因此,我不能真正设计我的 RegEx 来仅捕获 ESQ。例如,它可能是 ABC 或 XYZ。但是,它将保留为 3 个字母的单词。任何有用的输入将不胜感激。

此外,这篇文章也没有太大帮助:PHP Regex return array with values empty

笔记:

options.xml 

没有结束,'因为它不应该!

4

3 回答 3

1
[a-zA-Z]_Proof

应该

[a-zA-Z]+_Proof
于 2013-01-04T17:09:33.237 回答
1

您在问题中提供的正则表达式模式和文件数据不会导致空数组。至少不适合我(稍后阅读)。有了preg_match_all我会正确地得到一场比赛。我用过这段代码:

$file = <<<FILE
================================================
Header of File with time and date
================================================
Loaded options from XML file: '/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/ID2PDF_options.xml
extendedPrintPDF started
Postfix '3.0' was append from file 'ESQ030112ELAM_lo-metadata.xml' for file: '/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/someFile.indd
printPDF started
PDF Export Preset: Some preset
PDF file created: ''/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/someFile.pdf'.
File someFileName.xml removed
postprocessingDocument started
INDD file removed: '/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/someFile.indd
FILE;

$pattern = '/^Loaded options from XML file: \'\/.*\/SM_Folder\/([a-zA-Z]{3})\/[a-zA-Z]+_Proof_\1\/processing\/ID2PDF_options.xml$/im';

$result = preg_match_all($pattern, $file, $matches);

var_dump($result, $matches);

结果:

int(1)
array(2) {
  [0] =>
  array(1) {
    [0] =>
    string(127) "Loaded options from XML file: '/Thisis/some/Users/sumuser/Desktop/SM_Folder/ESQ/Virtual_Proof_ESQ/processing/ID2PDF_options.xml"
  }
  [1] =>
  array(1) {
    [0] =>
    string(3) "ESQ"
  }
}

您可能会得到类似于以下的结果(这也是与上面完全相同的代码,但在不同的计算机上作为演示这里):

int(0)
array(2) {
  [0]=>
  array(0) {
  }
  [1]=>
  array(0) {
  }
}

如果你得到这个结果,那么这表明多行模式下的^and$与行尾不匹配,因为你没有\n那里,但很可能是 CRLF 序列(DOS/Windows 行尾)。您可以使用以下ANYCRLF选项获取所有这些序列:

$pattern = '/(*ANYCRLF)^Loaded options from XML file: \'\/.*\/SM_Folder\/([a-zA-Z]{3})\/[a-zA-Z]+_Proof_\1\/processing\/ID2PDF_options.xml$/im';
             ^^^^^^^^^^

这应该会给你结果。请参阅工作演示

于 2013-01-04T20:11:49.117 回答
0

\/([a-zA-Z]{3})不是有效的正则表达式。您缺少分隔符。

preg_match_all(":\/([a-zA-Z]{3}):", $input, $matches);

你可以选择任何字符,这里我选择了:

于 2013-01-04T17:09:21.120 回答