douwen1929 2013-01-04 17:05
浏览 23
已采纳

RegEx返回一个空数组

So, I'm trying to catch 3 letter word from a text file. I've created a RegEx, but it return an EMPTY array. And I'm not able to figure out why! Here's a part of the text 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

Here's the RegEx I have:

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

If I remove \ in front of \/([a-zA-Z]{3}), I get an Unknown modifier:( error.

Can someone please tell me what I need to do to grab the "ESQ" from the first line of the record? The 3 letter word would be different in other records and so, I can't really design my RegEx to catch only ESQ. It might be ABC or XYZ for example. But, it would remain as a 3 letter word. Any useful inputs would be appreciated.

Also, this post didnt help much either: PHP Regex returning array with values empty

Note:

options.xml 

doesn't end with ' because it is not supposed to!

  • 写回答

3条回答 默认 最新

  • doujiao1905 2013-01-04 20:11
    关注

    The regular expression pattern and the file-data you have provided in your question are not resulting in an empty array. At least not for me (read on later). With preg_match_all I'll get one match properly. I've using this code:

    $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);
    

    Result:

    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"
      }
    }
    

    You probably get a result similar as the following (which is also by the exact same code as above but on a different computer as demo here):

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

    If you get this result, than this is a sign that the ^ and $ in multiline mode do not match the end of lines because you don't have there but most likely the CRLF sequence (DOS/Windows line-ending). You can take all these sequences by using the ANYCRLF option:

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

    This then should give you the results. See the working demo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?