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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looking for a regex to see if string contains '342' in it anywhere...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
0
0
basically i want this:

string='574678957895724343268746934687';

and i want to see is 342 pattern exists anywhere in there.

any ideas?

- g
 

so this works?

if ($string =~ /$offerID/) {print 'match';}

seems to match when isn't true sometimes. $offerID format is 1234, or 1345|2545|6777.

- g
 
Are you trying to match a literal pipe (|) in 1345|2545|6777, or is that an alternation (or) operator? If it's the former (literal), you'll need to backslash it so Perl doesn't interpret as alternation.

If that doesn't straighten it out (or I'm not making myself clear), post some code with some sample input.

 
$string='574678957895724343268746934687';

if( $string =~ /342/ ){
[tab]print "match\n";
} else {
[tab]print "no match\n";
}


Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
If you're only matching literal strings, it's easier to use the `index' function, rather than firing up the regexp engine (plus you don't have to worry about special regexp characters, not that that's a particularly onerous task either):
Code:
my $string = '574678957895724343268746934687';
my $offer_id = '342';
if ( index( $string, $offer_id ) >= 0 ) {
  print "match\n";
}
else {
   print "no match\n";
]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top