regexp question

Discussion in 'Perl Misc' started by Bart van den Burg, Sep 1, 2003.

  1. Hi

    I have the following regexp:
    $message =~ s/\[quote ?([^\]]*)](.*)\[\/quote]/<DIV STYLE='border: 1px solid
    black; padding: 2px; margin: 2px'>Quote $1:<DIV STYLE='font-size: .9em;
    color: #444444; margin-left: 5px;'>$2<\/DIV><\/DIV>/;

    It works on the following:
    ----------------------------------------------------
    [quote Bart]testerdetest[/quote] ja ach...
    ----------------------------------------------------

    But not on this:
    ----------------------------------------------------
    [quote Bart][...]

    test[/quote]

    TEST!!!
    ----------------------------------------------------

    What am I forgetting/doing wrong?

    Thanks
    Bart
     
    Bart van den Burg, Sep 1, 2003
    #1
    1. Advertising

  2. Bart van den Burg wrote:
    > I have the following regexp:
    > $message =~ s/\[quote ?([^\]]*)](.*)\[\/quote]/<DIV STYLE='border: 1px solid
    > black; padding: 2px; margin: 2px'>Quote $1:<DIV STYLE='font-size: .9em;
    > color: #444444; margin-left: 5px;'>$2<\/DIV><\/DIV>/;
    >
    > It works on the following:
    > ----------------------------------------------------
    > [quote Bart]testerdetest[/quote] ja ach...
    > ----------------------------------------------------
    >
    > But not on this:
    > ----------------------------------------------------
    > [quote Bart][...]
    >
    > test[/quote]
    >
    > TEST!!!
    > ----------------------------------------------------
    >
    > What am I forgetting/doing wrong?


    You are forgetting the /s modifier.

    If there are more than one such sections in the text
    you are applying the regexp to, you'd better make it non-greedy and
    add the /g modifier:

    s/\[quote ?([^\]]*)](.*?)\[\/quote]/.../gs;
    ---------------------------^----------------^^

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 1, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    164
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    495
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    367
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    194
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    183
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page