Regular Expressions

J

John Baro

Apologies if this is the wrong group.

I want to match 2 or more items within a string

ie match string is "abc"
To match
"abc" -no match
"abcabc" -match
"abcdefabc" -match
"abcabcabc" -match

Any help would be appreciated.
I could get each match of abc individually and check if there are two or
more but would prefer to execute in one statement.

Cheers
JB
 
B

Ben Morrow

Quoth "John Baro said:
Apologies if this is the wrong group.

I want to match 2 or more items within a string

ie match string is "abc"
To match
"abc" -no match
"abcabc" -match
"abcdefabc" -match
"abcabcabc" -match

Any help would be appreciated.
I could get each match of abc individually and check if there are two or
more but would prefer to execute in one statement.

/(?: abc .* ){2,}/x

Ben
 
M

Malcolm Dew-Jones

John Baro ([email protected]) wrote:
: Apologies if this is the wrong group.

: I want to match 2 or more items within a string

: ie match string is "abc"
: To match
: "abc" -no match
: "abcabc" -match
: "abcdefabc" -match
: "abcabcabc" -match

: Any help would be appreciated.
: I could get each match of abc individually and check if there are two or
: more but would prefer to execute in one statement.

One possibility (not necessarily the best) but flexible.

$string = "abc";

$To_match="abc";
print "To_Match=$To_match\n";
print "$To_match matches $string\n"
if $To_match =~ s/\Q$string/$string/g >= 2;

$To_match="abcabc" ;
print "To_Match=$To_match\n";
print "$To_match matches $string\n"
if $To_match =~ s/\Q$string/$string/g >= 2;

$To_match="abcdefabc" ;
print "To_Match=$To_match\n";
print "$To_match matches $string\n"
if $To_match =~ s/\Q$string/$string/g >= 2;

$To_match="abcabcabc" ;
print "To_Match=$To_match\n";
print "$To_match matches $string\n"
if $To_match =~ s/\Q$string/$string/g >= 2;
 
U

Uri Guttman

TA> I'm no regular expression expert, but won't this be enough?

TA> /(abc){2,}/

that will match 'abcabcabc'. but what about the possible stuff between
the 'abc's? that is what the .* does. the ?: is just an efficiency thing
making sure no grabbing done since it is not needed.

uri
 
J

Jeff Schwab

John said:
Apologies if this is the wrong group.

I want to match 2 or more items within a string

ie match string is "abc"
To match
"abc" -no match
"abcabc" -match
"abcdefabc" -match
"abcabcabc" -match

Any help would be appreciated.
I could get each match of abc individually and check if there are two or
more but would prefer to execute in one statement.

Cheers
JB

sub matches_at_least_twice( $$ ) { (my @a = $_[0] =~ /$_[1]/g) > 1 }
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top