Why does my regular expression not work?

Discussion in 'Perl Misc' started by Thomas Werzmirzowsky, Aug 12, 2005.

  1. Hi,

    i need a regular expression that matches (for example) the following
    strings:

    $$[mark] fkajdfka $$[/mark]
    $$[ mark ] dfalkdfj $$[ /mark ]
    $$[abc ] dkjfakldf $$[ / abc ]
    and so on.

    To do this i "designed" this regular expression (spaces are just for
    better reading):
    \$ \$ \[ \s* (.+?) \s* \] .+? \$ \$ \[ \s* \/ \s* \1 \s* \]
    And it works :)

    But now i want that befor the <mark> in the first $$[ ] can be a !.
    So i changed my expression to:
    \$ \$ \[ \s* (!?) \s* (.+?) \s* \] .+? \$ \$ \[ \s* \/ \s* \2 \s* \]
    And it doesn't work :(

    I even tried (\!?) instead of (!?) but this doesn't work, too :(

    Can anybody tell me why the second expression doesn't work?

    Thanks in advance :)
    Thomas W
     
    Thomas Werzmirzowsky, Aug 12, 2005
    #1
    1. Advertising

  2. Thomas Werzmirzowsky

    Dave Weaver Guest

    On 12 Aug 2005 01:23:19 -0700, Thomas Werzmirzowsky <> wrote:
    > Hi,
    >
    > i need a regular expression that matches (for example) the following
    > strings:
    >
    > $$[mark] fkajdfka $$[/mark]
    > $$[ mark ] dfalkdfj $$[ /mark ]
    > $$[abc ] dkjfakldf $$[ / abc ]
    > and so on.
    >
    > To do this i "designed" this regular expression (spaces are just for
    > better reading):
    > \$ \$ \[ \s* (.+?) \s* \] .+? \$ \$ \[ \s* \/ \s* \1 \s* \]
    > And it works :)
    >
    > But now i want that befor the <mark> in the first $$[ ] can be a !.
    > So i changed my expression to:
    > \$ \$ \[ \s* (!?) \s* (.+?) \s* \] .+? \$ \$ \[ \s* \/ \s* \2 \s* \]
    > And it doesn't work :(



    "doesn't work" is the worst problem description ever!
    What does it do that it shouldn't? What doesn't it do that it should?
    If it doesn't match some data that you expect it to match, what data
    is that?

    Have you seen the posting guidelines for this group that are posted
    here regularly? They provide information that will help you compose
    your question in a fashion most likely to get you useful help.
    One piece of advice there is to post a *short* but *complete* program
    that demonstrates your problem - that way potential helpers can
    cut-and-paste it to help you out with minimal fuss.


    > Can anybody tell me why the second expression doesn't work?


    It seems to work for me.

    #!/usr/bin/perl
    use strict;
    use warnings;

    while ( <DATA> ) {
    chomp;
    print;
    if ( /\$\$\[\s*(!?)\s*(.+?)\s*\].+?\$\$\[\s*\/\s*\2\s*\]/ ) {
    print " : match\n";
    }
    else {
    print " : no match\n";
    }
    }

    __DATA__
    $$[mark] fkajdfka $$[/mark]
    $$[ mark ] dfalkdfj $$[ /mark ]
    $$[ mark ] dfalkdfj $$[ /abc ]
    $$[#abc ] dkjfakldf $$[ / abc ]
    $$[!mark] fkajdfka $$[/mark]
    $$[ !mark ] dfalkdfj $$[ /mark ]
    $$[ ! abc ] dkjfakldf $$[ / abc ]
    $$[ # abc ] dkjfakldf $$[ / abc ]


    output:

    $$[mark] fkajdfka $$[/mark] : match
    $$[ mark ] dfalkdfj $$[ /mark ] : match
    $$[ mark ] dfalkdfj $$[ /abc ] : no match
    $$[#abc ] dkjfakldf $$[ / abc ] : no match
    $$[!mark] fkajdfka $$[/mark] : match
    $$[ !mark ] dfalkdfj $$[ /mark ] : match
    $$[ ! abc ] dkjfakldf $$[ / abc ] : match
    $$[ # abc ] dkjfakldf $$[ / abc ] : no match
     
    Dave Weaver, Aug 12, 2005
    #2
    1. Advertising

  3. > "doesn't work" is the worst problem description ever!

    Sorry...
    It didn't match the text that it should match!

    I already solved my problem (i didn't have the time to post it
    earlier). I had a mistake in another part of my code.

    Thanks for your help.
    Thomas W
     
    Thomas Werzmirzowsky, Aug 14, 2005
    #3
    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. VSK
    Replies:
    2
    Views:
    2,396
  2. Johann Sijpkes
    Replies:
    2
    Views:
    476
    Johann Sijpkes
    Jul 14, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,253
    Smokey Grindel
    Dec 2, 2006
  4. Virtual Buddha
    Replies:
    3
    Views:
    362
    Virtual Buddha
    Jun 27, 2009
  5. Stefan Rupp
    Replies:
    1
    Views:
    97
    Stefan Rupp
    Sep 20, 2003
Loading...

Share This Page