Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex Help

Status
Not open for further replies.

Zoom1234

Programmer
Oct 30, 2003
116
BE
Hi,

Code:
$group = "A,B,C,D" ;
$str = "A,D" ;
I want to see if $group contains letters A and D.
How is it possible without splitting $str?

TIA
 
Don't think so. You'd have to split $str on ',' and check each one individually.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Kirsle

I have to match both A and D here.So simple if ($group =~ /$str/) won't work here.

Any ideas or as Steve says its not possible?
 
Ohh, sorry, I misread your post.

You can try it in two different regexps:

Code:
if ($group =~ /A/ && $group =~ /D/) {
   print "$group contains A and D\n";
}
 
Here's a different way, that doesn't use a split, but the concept is almost the same as a split... Probably not what you need, but maybe it could help:

Code:
#!/usr/bin/perl -w
use strict;
my $group = "A,B,C,D" ;
my $str = "A,F,D,Z,B" ;
$str=~s/,/|/g;
my $re=qr($str);
my $matched=join(',' , $group=~/$re/g);
print "Matched on: $matched\n";
The output:
Code:
Matched on: A,B,D

Basically, I am converting the commas to pipes (the "OR" for a regex), and building a regex from that string.

Don't know if that works or not, but like I said, maybe a step in the right direction?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top