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

easy reg expression question that I know you can help me with

Status
Not open for further replies.

iggystar

IS-IT--Management
Jul 12, 2001
126
US
Ok little script here:

$in = <>;
if ($in =~ /p*o*b*o*x/i) {
print &quot;MATCH&quot;
}



Simple enough huh? Here the problem, if I feed it

po xob

it still counts as a match. I don't want it to. I want to match p o b o x, with any amount of characters between them, but I want them to be in that order only.

Thanks. I'm sure it'll take you guys like 8 seconds to tell me what silly mistake I'm making.
 
The asterisk(*) says &quot;match 0 or more of the previous character&quot; - notice the &quot;of the previous character&quot; part. You probably want to put a dot(.) in front of each asterisk:

if ($in =~ /p.*o.*b.*o.*x/i) {

the dot(.) says match *any* single character, so the .*(dot asterisk) says match any single character 0 or more times.

Read the great perldocs info on regular expressions by doing

perldoc perlre

at a command prompt.

HTH. Hardy Merrill
 
Thanks. Never seen this:

perldoc perlre

before. I'll give it a try tomorrow.
 
I dont know if this will work but try replacing the * with a +, to say match 1 or more of the chracter, not 0 or more, if you want to match the exact string 'p o b o x' try if ($in =~ /p\so\sb\so\sx\){print &quot;MATCH&quot;}, the \s says to match white space, so if you want to match just &quot;pobox&quot; drop the \s and the +, there is no need to match anymore than one of the given chracters in pobox.

Hope this helps skottieb:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top