Why does my regular expression not work?

  • Thread starter Thomas Werzmirzowsky
  • Start date
T

Thomas Werzmirzowsky

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
 
D

Dave Weaver

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
 
T

Thomas Werzmirzowsky

"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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top