Reputation: 396
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!
Upvotes: 0
Views: 882
Reputation: 197777
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 \n
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.
Upvotes: 1
Reputation: 57729
\/([a-zA-Z]{3})
is not a valid regular expression. You're missing delimiters.
preg_match_all(":\/([a-zA-Z]{3}):", $input, $matches);
You can pick any character, here I chose :
.
Upvotes: 0