Catching a 3 letter word from a text file using RegEx

Discussion in 'PHP' started by newphpuser, Jan 4, 2013.

  1. #1
    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.

    ================================================
    [Jan 25 2012 11:26:03] - ID2PDF v.2.6 (ID2PDF.jsx)
    General options: [default] (ID2PDF_options.xml)
    ================================================
    Loaded options from XML file: '/This/folder/Users/userName/Desktop/SoMe_Folder/ABC/SomeName_Proof_ABC/processing/someFile_options.xml
    extendedPrintPDF started
    Postfix '4.4' was append from file 'ABCFileName.xml' for file: /This/folder/Users/userName/Desktop/SoMe_Folder/ABC/SomeName_Proof_ABC/processing/ABCFileName.indd
    PDF file created: '/This/folder/Users/userName/Desktop/SoMe_Folder/ABC/SomeName_Proof_ABC/processing/ABCFileName.4.pdf'.
    INDD file removed: /This/folder/Users/userName/Desktop/SoMe_Folder/ABC/SomeName_Proof_ABC/processing/ABCFileName.indd
    ================================================
    [Jan 25 2012 11:44:50] - ID2PDF v.2.6 (ID2PDF.jsx)
    General options: [default] (ID2PDF_options.xml)
    ================================================
    Loaded options from XML file: '/This/folder/Users/userName/Desktop/SoMe_Folder/XYZ/SomeOtherName_Proof_XYZ/processing/ID2PDF_options.xml
    extendedPrintPDF started
    Postfix '4.2' was append from file 'XYZFileName.xml' for file: /This/folder/Users/userName/Desktop/SoMe_Folder/XYZ/SomeOtherName_Proof_XYZ/processing/XYZFileName.indd
    PDF file created: '/This/folder/Users/userName/Desktop/SoMe_Folder/XYZ/SomeOtherName_Proof_XYZ/processing/XYZFileName.2.pdf'.
    INDD file removed: /This/folder/Users/userName/Desktop/SoMe_Folder/XYZ/SomeOtherName_Proof_XYZ/processing/XYZFileName.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
    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.
    Any useful inputs would be appreciated.

    Note: Can someone please help!! :(
     
    Last edited: Jan 4, 2013
    newphpuser, Jan 4, 2013 IP
  2. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #2
    So, to make sure I understand you correctly, it's not the contents of the file, but the letters in the path?

    So, from:

    "Loaded options from XML file: '/This/folder/Users/userName/Desktop/SoMe_Folder/ABC/SomeName_Proof_ABC/processing/someFile_options.xml'"

    You want ABC (or whatever it will be)?
     
    ryan_uk, Jan 4, 2013 IP
  3. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #3
    Assuming so, then:

    $pattern = "/^Loaded options from XML file: '\/(.*)\/SoMe_Folder\/([a-zA-Z]{3})\/(.*)_Proof_([a-zA-Z]{3})\/processing\/someFile_options\.xml'/im";
    $string = "Loaded options from XML file: '/This/folder/Users/userName/Desktop/SoMe_Folder/ABC/SomeName_Proof_ABC/processing/someFile_options.xml'";
    
    preg_match($pattern, $string, $matches);
    
    print_r($matches);
    PHP:
    Will produce:

    Array ( [0] => Loaded options from XML file: '/This/folder/Users/userName/Desktop/SoMe_Folder/ABC/SomeName_Proof_ABC/processing/someFile_options.xml' [1] => This/folder/Users/userName/Desktop [2] => ABC [3] => SomeName [4] => ABC )
    PHP:
     
    ryan_uk, Jan 4, 2013 IP
  4. newphpuser

    newphpuser Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Haven't tried this yet. But, I found 1 more solution. :)
    Thanks a ton for your help though! I'll keep this solution as a backup for myself.
     
    newphpuser, Jan 4, 2013 IP