Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Multiline pattern matching

by Anonymous Monk
on May 21, 2001 at 18:09 UTC ( #81970=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I match the following?
foo { ... ... ... };
Thanks in advance

Replies are listed 'Best First'.
Re: Multiline pattern matching
by Beatnik (Parson) on May 21, 2001 at 18:17 UTC
    Your description is rather vague... I'll assume
    foo { ... ... ... };
    is in a string somehow... something like : $foo =~ /foo {[^};]+};/s; should be ok...

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
(tye)Re: Multiline pattern matching
by tye (Sage) on May 21, 2001 at 21:33 UTC

    If the part you left out can contain matching pairs of braces:

    foo { bar { }; };
    then I encourage you to try Parse::RecDescent. If the part you left out can also contain braces that are quoted or escaped in some way:
    foo { bar "No closing } found!" };
    then I strongly encourage you to try Parse::RecDescent.

            - tye (but my friends call me "Tye")
Re: Multiline pattern matching
by alfie (Pilgrim) on May 21, 2001 at 18:18 UTC
    Please read perlman:perlre. Your question is too vague to be answered but you should be able to find what you are looking for in this page.
    --
    use signature; signature(" So long\nAlfie");
Re: Multiline pattern matching
by jeroenes (Priest) on May 21, 2001 at 19:02 UTC
    Your problem description is somewhat minimal, but your title suggests that you are looking for the 's' modifier. So make your regex like:
    $var =~ m/some text\nand more/s;
    The last 's' makes sure all text is read like one string, even if it contains newlines.

    Of course you could have read this in perlop and prelre.

    Hope this helps,

    Jeroen
    "We are not alone"(FZ)

Re: Multiline pattern matching
by blue_cowdawg (Monsignor) on May 21, 2001 at 18:22 UTC

    Base Assumption: Your text you are trying to match is somehow in one single variable.

    $str =~ m/foo\{(.*)\}\;/;

    $1 is now going to contain everything between the curly braces. The flaw I see in my own pattern matching is the fact that if you have another "};" along the way you might not have what you are after.

    Then again.. you were pretty vague about your specifications... ;-)

    
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Peter L. Berghold --- Peter@Berghold.Net
    "Those who fail to learn from history are condemned to repeat it."
    
      Sorry, blue_cowdawg, you do a common mistake: You used dot star - please read Death to Dot Star! which explains why you should never ever use it :-)
      --
      use signature; signature(" So long\nAlfie");

        Your Death to Dot Star post is rather convincing!

        I like merlyn's comment concerning inching along though. If I really had to write something to parse foo { ... }; I'd probably use that.

        What I find really intriguing however is the original question posed however vague. Given a Perl'ish structure of text where you have a symbol followed by a open curly brace followed by more text with possibly more curly braces (open and closed) how do you go about matching the right pair?

        My first instinct would be to look towards some sort of recursive parser and not a regex. What are your thoughts?

        
        ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        Peter L. Berghold --- Peter@Berghold.Net
        "Those who fail to learn from history are condemned to repeat it."
        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://81970]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2022-05-25 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?