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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行