Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
-
Contact:
Post
by raghavan20 »
i have blocks like this in a file which have to be extracted.
Code: Select all
LIN+1+3+9780194346092:EN++2'
QTY+52:100'
DTM+44:19971009:102'
FTX+LIN++IP:8B:28'
TAX+7+VAT+++:::17.5+S'
MOA+125:21.6'
PRI+AAE:25.38::SRP'
ALC+A+EQ'
LIN+2+3+9780192717931:EN++2'
QTY+52:0'
DTM+44:19980521:102'
FTX+LIN++OP:8B:28'
TAX+7+VAT++++Z'
PRI+AAE:12.99::SRP'
ALC+A+CK'
i want to take in blocks of ( LIN .... ALC..' )
my code is
Code: Select all
$regex = '#\bLIN.*?ALC.*?\'#mi';
preg_match_all( $regex, $fileContents, $blocks );
print_r( $blocks );
can anyone see what is wrong in there?
-
feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Post
by feyd »
"m" (multiline) mode makes it look for a single line.
-
Ciprian
- Forum Newbie
- Posts: 4
- Joined: Fri Feb 02, 2007 10:15 pm
Post
by Ciprian »
here is something that should work:
$regex = '#^LIN.*|ALC.*\'#im';
Have fun.
-
raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
-
Contact:
Post
by raghavan20 »
Ciprian wrote:here is something that should work:
$regex = '#^LIN.*|ALC.*\'#im';
Have fun.
i do not want LIN or ALC lines but i want from LIN to ALC lines.
this file has a lot of code and i have to extract all blocks of LIN to ALC lines.
-
raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
-
Contact:
Post
by raghavan20 »
feyd wrote:"m" (multiline) mode makes it look for a single line.
even if i take m off, it still does not work feyd.
-
feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Post
by feyd »
The default is multiline mode. You want the "s" modifier.
-
raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
-
Contact:
Post
by raghavan20 »
feyd wrote:The default is multiline mode. You want the "s" modifier.
oh yes feyd, it worked.
its been a long time i worked with regex, i forgot the modifier character. Thank you.
-
Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Post
by Ollie Saunders »
You probably want to wrap the .*? in () too