Sqlspider
asked on
How to use Preg_match to get string that continues on next line
Hello all again.
I was given help on a topic earlier that gave the following:
Thank you again gr8gonzo on the help. The code works great until it come across string that wraps to the nextline.
example:
89245*000087726*01*0520001 13*DA*1879 0741*20120 312~T
RN*1*1QG70794930*1411289245*000087726
I tried to play with regular expressions and was unable to get the Part in bold to work when the line wraps or carriage returns. I also tried doing
echo substr($str,124,11) . "<br /> <br />"; but even though that gave me what I was looking for in the case above it fail for other files.
NON************20120312~T
RN*1*1QG70795851*1411289245*
I could not find any sites that explicitly showed how to use the regular expressions.
Thanks for any help with this.
I was given help on a topic earlier that gave the following:
if(preg_match[b]("/(~TRN\*[^\*]+\*[^\*]+)[/b]/",file_get_contents($oldfile),$matches))
	 {
	 // Match found! Renaming file!
$newfile = str_replace(array(".txt","*"),array("_","-"),$oldfile.$matches[0]).".txt";
echo "Renaming file from {$oldfile} to {$newfile}...". "<br /> <br />";
}
else
{
// No match found!
}
}
Thank you again gr8gonzo on the help. The code works great until it come across string that wraps to the nextline.
example:
89245*000087726*01*0520001
RN*1*1QG70794930*1411289245*000087726
I tried to play with regular expressions and was unable to get the Part in bold to work when the line wraps or carriage returns. I also tried doing
echo substr($str,124,11) . "<br /> <br />"; but even though that gave me what I was looking for in the case above it fail for other files.
NON************20120312~T
RN*1*1QG70795851*1411289245*
I could not find any sites that explicitly showed how to use the regular expressions.
Thanks for any help with this.
If you are looking for the regular expression syntax for finding carriage returns I believe its \r
So replace the replacement line with this.
$newfile = str_replace(array(".txt"," *","\r","\ n"),array( "_","-","" ,""),$oldf ile.$match es[0]).".t xt";
That will replace any carriage returns with nothing (or delete them)
And \n for New Line gets replaced with nothing as well.
$newfile = str_replace(array(".txt","
That will replace any carriage returns with nothing (or delete them)
And \n for New Line gets replaced with nothing as well.
ASKER
Ok I understand you comment but the problem is with preg_match. for example.
If i take the text
preg_match ("/(~TRN\*[^\*]+\*[^\*]+)/ ") will not grab ~T and then pick up the rest of the pattern.
If the text is the following:
89245*000087726*01*0520001 13*DA*1879 0741*20120 312
~TRN*1*1QG70794930*1411289 245*000087 726
Preg_match ("/(~TRN\*[^\*]+\*[^\*]+)/ ") captures the pattern just fine.
If i take the text
89245*000087726*01*052000113*DA*18790741*20120312~T
RN*1*1QG70794930*1411289245*000087726
preg_match ("/(~TRN\*[^\*]+\*[^\*]+)/
If the text is the following:
89245*000087726*01*0520001
~TRN*1*1QG70794930*1411289
Preg_match ("/(~TRN\*[^\*]+\*[^\*]+)/
Well then just do a replace function on the $oldfile before doing the preg_match check
Like this
Like this
$fileContents = file_get_contents($oldfile);
$fileContents = str_replace(array("\r","\n"),array("","");
if(preg_match[b]("/(~TRN\*[^\*]+\*[^\*]+)[/b]/",$fileContents,$matches))
{
// Match found! Renaming file!
$newfile = str_replace(array(".txt","*"),array("_","-"),$oldfile.$matches[0]).".txt";
echo "Renaming file from {$oldfile} to {$newfile}...". "<br /> <br />";
}
else
{
// No match found!
}
}
ASKER
Ok I tried that and I get this
Parse error: syntax error, unexpected ';'
for this section of the code
$fileContents = str_replace(array("\r","\n "),array(" ","");
Yes I do like the way you put it but not sure why it is not working.
Parse error: syntax error, unexpected ';'
for this section of the code
$fileContents = str_replace(array("\r","\n
Yes I do like the way you put it but not sure why it is not working.
Sorry about that its missing a closing ) in the line!
$fileContents = str_replace(array("\r","\n "),array(" ",""));
$fileContents = str_replace(array("\r","\n
ASKER
Ok I tried what was stated maestropsm but maybe I am missing something. I was successful with the following until I hit another issue.
The change I made was in the pre_match if(preg_match("/((~TR|N)\* [^\*]+\*[^ \*]+)/",$f ileContent s,$matches ))
This seemed to fix a few issues but not all.
Again in the txt file I am trying to capture the pattern that matches ~TRN*1*1Q54896*.
Please note that the *1Q54896* portion can vary. I was successful in getting the echo to show on the screen properly but when I do the rename($oldfile,$newfile) I get the following on a few files
// Get an array of all files in the current directory that end in .txt
$txtfiles = glob("c:\splitting\*.txt");
// Loop through the files
foreach($txtfiles as $oldfile)
{
// Skip files that already look like they've been processed
if(strpos($oldfile,"_~")) continue;
// Otherwise, process the rest of the files
$fileContents = file_get_contents($oldfile);
if(preg_match("/((~TR|N)\*[^\*]+\*[^\*]+)/",$fileContents,$matches))
{
// Match found! Renaming file!
$newfile = str_replace(array(".txt",""),array("_","_"),$oldfile.$matches[1]).".txt";
echo "Renaming file from {$oldfile} to {$newfile}...". "<br /> <br />";
rename($oldfile,$newfile);
}
else
{
echo 'No match found!'. "<br /> <br />";
}
}
The change I made was in the pre_match if(preg_match("/((~TR|N)\*
This seemed to fix a few issues but not all.
Again in the txt file I am trying to capture the pattern that matches ~TRN*1*1Q54896*.
Please note that the *1Q54896* portion can vary. I was successful in getting the echo to show on the screen properly but when I do the rename($oldfile,$newfile) I get the following on a few files
Warning: rename(c:\splitting\ERA_uhc_batch 1 to 22_20120313_013022_S_ER073560_16.txt,c:\splitting\ERA_uhc_batch 1 to 22_20120313_013022_S_ER073560_16_N_1 _1QG80786469.txt) [function.rename]: The filename, directory name, or volume label syntax is incorrect. (code: 123)
. Uncertain what happened here.
ASKER
Any help pls on this issue.
I'm not the worlds biggest regular expression expert but I think this might do it.
if(preg_match("/((~TRN|~TR \rN|~TR\nN |~T\rRN|~T \nRN|~\rTR N|~\nTRN)\ *[^\*]+\*[ ^\*]+)/",$ fileConten ts,$matche s))
Replace your regular expression with the above.
In the first condition using | as an or statement - I believe this will match any of the possible iterations of the ~TRN with spaces or carriage returns in between those letters and then continue through the remainder of the expression.
if(preg_match("/((~TRN|~TR
Replace your regular expression with the above.
In the first condition using | as an or statement - I believe this will match any of the possible iterations of the ~TRN with spaces or carriage returns in between those letters and then continue through the remainder of the expression.
I'm no Regex expert but doesn't the '~' need to be escaped? '\~'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for the help. Ray_Paseur's solution is what worked. I did have to do a bit more fine tuning but the example you gave in the str_replace is what i used to cause the text to populate on its own line. Once I did that I used the Preg_match without the added Regex and it worked.
Again you all are Top's in my book. I am trying more and more to get as good as you all are!!!!
Again you all are Top's in my book. I am trying more and more to get as good as you all are!!!!
Thanks for the points and thanks for using EE! This was a great question. All the best, ~Ray