[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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
0
RaineyM
Asked:
RaineyM
  • 7
  • 4
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now