chrism01 has asked for the wisdom of the Perl Monks concerning the following question:
Monks
conceptually, i'd like to match any value of string $str1 that starts (exactly) with another string ($str2), both stored in variables eg:
$str2 = ".1.2.3.4";
I'd like it to match if:
$str1 =".1.2.3.4 lhkfjd";
but not if $str1 is any of
"x1.2.3.4 lhkfjd";
" 1.2.3.4 lhkfjd";
"..1.2.3.4 lhkfjd";
etc
Unfortunately, I can't seem to get the syntax quite correct, although i have looked at various perl regex tutorials, inc at perldoc.
Solution with explanation deeply appreciated
Cheers
Chris
conceptually, i'd like to match any value of string $str1 that starts (exactly) with another string ($str2), both stored in variables eg:
Given:if( $str1 =~ /^$str2/ ) { print "match\n"; } else { print "nomatch\n"; }
$str2 = ".1.2.3.4";
I'd like it to match if:
$str1 =".1.2.3.4 lhkfjd";
but not if $str1 is any of
"x1.2.3.4 lhkfjd";
" 1.2.3.4 lhkfjd";
"..1.2.3.4 lhkfjd";
etc
Unfortunately, I can't seem to get the syntax quite correct, although i have looked at various perl regex tutorials, inc at perldoc.
Solution with explanation deeply appreciated
Cheers
Chris
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regex Start Anchor with variables
by Samy_rio (Vicar) on Oct 05, 2006 at 05:52 UTC | |
Re: Regex Start Anchor with variables
by jwkrahn (Abbot) on Oct 05, 2006 at 05:55 UTC | |
Re: Regex Start Anchor with variables
by ikegami (Patriarch) on Oct 05, 2006 at 06:13 UTC | |
Re: Regex Start Anchor with variables
by japhy (Canon) on Oct 05, 2006 at 12:20 UTC | |
by Hue-Bond (Priest) on Oct 05, 2006 at 13:26 UTC | |
by japhy (Canon) on Oct 05, 2006 at 20:07 UTC | |
Re: Regex Start Anchor with variables
by chrism01 (Friar) on Oct 05, 2006 at 06:05 UTC | |
by Hofmator (Curate) on Oct 05, 2006 at 12:20 UTC |
Back to
Seekers of Perl Wisdom