CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Catching optional newlines in .*?

 



Danni
Novice

Nov 16, 2002, 2:20 PM

Post #1 of 5 (4216 views)
Catching optional newlines in .*? Can't Post

Ive gotten a little stuck trying to figure this out. This is part of what will be a very large complex regex. I need the last (.*) to capture the whole description but it breaks on newlines. Any clues?




Code
while ($catalog =~ / 
(.*?)\n # capture title
(.*?)\sCatalog\s\#:\s(.*?)\sList:\s(.*?)\sSale:\s(.*?)\n # capture other stuff
(.*)\n # Capture description
/gxm) {
#do stuff
}



This needs to match the following


Quote
Anime Keychain Assortment
KEY Catalog #: PACK1202 List: $18.00 Sale: $12.99
Four of the coolest Keychains out there. One each from Tenchi, DBZ,
Sailor Moon, and Cardcaptors. Start your collection today.
Arc the Lad SeriesSet
DVD Catalog #: PACK1209 List: $179.70 Sale: $99.99
Contains the entire series of Arc the Lad on DVD.
Battle Athletes Pack
DVD Catalog #: PACK1126 List: $164.78 Sale: $123.99
Contains all DVD volumes of the Battle Athletes Victory and Battle
Athletes series.



Paul
Enthusiast

Nov 17, 2002, 2:23 AM

Post #2 of 5 (4209 views)
Re: [Danni] Catching optional newlines in .*? [In reply to] Can't Post

Try changing gxm to sgxm


Danni
Novice

Nov 17, 2002, 10:49 AM

Post #3 of 5 (4205 views)
Re: [RedRum] Catching optional newlines in .*? [In reply to] Can't Post

Yes ive tried that, with the s modifier the first (.*?) captures too much, and so does the last. :s


jryan
User

Nov 18, 2002, 1:53 PM

Post #4 of 5 (4197 views)
Re: [Danni] Catching optional newlines in .*? [In reply to] Can't Post

It will be hard to figure out how to help you without seeing your data. This *might* fix your problem, but its hard to say without seeing the data first:


Code
while ($catalog =~ /  
([^\n]*)\n # capture title
(.*?)\sCatalog\s\#:\s(.*?)\sList:\s(.*?)\sSale:\s([^\n]*)\n # capture other stuff
(.*)\n # Capture description
/sgxm) {
#do stuff
}



davorg
Thaumaturge / Moderator

Nov 19, 2002, 12:07 AM

Post #5 of 5 (4193 views)
Re: [Danni] Catching optional newlines in .*? [In reply to] Can't Post

Then just turn /s on for the part of the regex where it's needed

Code
while ($catalog =~ /  
(.*?)\n # capture title
(.*?)\sCatalog\s\#:\s(.*?)\sList:\s(.*?)\sSale:\s(.*?)\n # capture other stuff
((?s).*)\n # Capture description
/gxm) {
#do stuff
}

See the section on "Extended Patterns" in perldoc perlre for more details.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives