Regular Expression Help

I am retrieving the following string from a MySQL database and need a regular expression to detect the [code]...[/code] parts. No matter what expression I use, it won't detect it.

This is a test.\n[code]\n<p>Hello</p>\n<p>World</p>\n[/code]\nThis is a test.\n
RaineyMAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

manav_mathurCommented:
Raineym,
use strict;
use warnings;
my $var="This is a test.\n[code]\n<p>Hello</p>\n<p>World</p>\n[/code]\nThis is a test.\n" ;
$var =~ s/^.*(\[code\].*\[\/code\]).*$/$1/si;
print $var

Manav
0
manav_mathurCommented:
This also gives you the [code] and the [\code] part.

If you dont want them,

use strict;
use warnings;
my $var="This is a test.\n[code]\n<p>Hello</p>\n<p>World</p>\n[/code]\nThis is a test.\n" ;
$var =~ s/^.*\[code\](.*)\[\/code\].*$/$1/si;
print $var

Manav
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sstoukCommented:
my($Test) = "\n[code]\n<p>Hello</p>\n<p>World</p>\n[/code]\nThis is a test.[code]\n<p>Part 2</p>\n[/code]\n";
my(@Parts) = undef;
while ($Test =~ m!\[code\](.*?)\[/code\]!gis)
{
      push @Parts, $1;      
};
print "There are ", scalar $#Parts, " parts detected...\n";
foreach (@Parts)
{
      print "\n$_";
};
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

manav_mathurCommented:
Raineym,

the important thing in all these solutions is the use of /s

s - enables multiline match

Manav
0
RaineyMAuthor Commented:
Thank you Manav.

I can't believe I forgot /s. Doh!
0
manav_mathurCommented:
Happens ;)

Manav
0
RaineyMAuthor Commented:
Ok, now when there is more than one [code]...[/code] in the string, it detects everything between the first occurance of [code] and the last occurance of [/code].
0
manav_mathurCommented:
No no

then use

use strict;
use warnings;
my $var="This is a test.\n[code]\n<p>Hello</p>\n<p>World</p>\n[/code]\nThis is a test.\n[code]\nHi second\n[/code]\nEnd\n" ;
while ($var =~ m/\[code\](.*?)\[\/code\]/gsi) {
print "detected" ;
print "$1" ;
}

Notice the .*? instead of the .* in  the regex.

A regex by default matches the largest string it can find. a ? before the regex overrides this default behaviour.
In your problem, hence you wnat to match the smallest string between two [code] and [\code]. Hence this will work

also remember to put the g(for global match).

Manav
0
manav_mathurCommented:
And if you want to have [code] and [\code] in $1 too, use

use strict;
use warnings;
my $var="This is a test.\n[code]\n<p>Hello</p>\n<p>World</p>\n[/code]\nThis is a test.\n[code]\nHi second\n[/code]\nEnd\n" ;
while ($var =~ m/(\[code\].*?\[\/code\])/gsi) {
print "detected\n" ;
print "$1" ;
}


Manav
0
RaineyMAuthor Commented:
That I did no know. ** Gray Matter Expanding **

Any way to give bonus points? You deserve it.
0
manav_mathurCommented:
Raineym,

You could put a request to community support. Or post another dummy question, then I can post a stupid answer to the dummy question and you award me the points. ;)

Actually, most of the ppl dont know this maximum matching property of regex. Hence, most of them dont know the use of a ? (apart from when used after a metacharacter)

.? -> matches 0 or 1 character
.*? -> matches the minimum number of any characters. This by itslef dosnt make much if a sense unless it is surronded by ohter fragments of regex on either side or both sides.

Manav
0
RaineyMAuthor Commented:
OK, look for the question "What is the maximum matching property of regex?".
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.