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!

using variables in regexps 2

Status
Not open for further replies.

expostfacto

Programmer
Sep 10, 2001
31
US
simple example:

$v = 'test';
if (' test ' =~ '/\\b$v\\b/') {
print 'booya';
}

(obviously matching against simply /\b$v\b/ won't work 'cause that'll get the RE meaning of $ rather than the scalar-access meaning.)

how can I do this?
 
Are you trying to see if $v matches 'test'?
Then it's

if ($v =~ /\btest\b/) {
print "booya\n"
}

or, without using a regex at all,

if ($v eq 'test') {
print "booya\n"
}

(You may not need the "\n" on the end of your
print statements, depending on your environment.)
 
no...

I just left the ' test ' in the regexp hardcoded for simplicity; in reality it's going to be a variable too.

in other words I want to see if variable $foo contains any single words matching variable $bar.
 
# This prints booyah if $foo contains any words matching
# the value of $bar.

$bar = "yeah";
$foo = "yeah, man";
if ($foo =~ /\b$bar\b/) {
print "booyah\n";
}
 
ack, i was sure that wouldn't work... guess the parser's smart enough to tell the difference from an eol here. teach my to test before posting. :( thanks!
 
Yeah, that's the great thing about Perl.

Note that in your original post, you've got 2 backslashes
where you only need one: word boundary is '\b', not '\\b',
unless you're writing Emacs Lisp.
 

If this is a reg expression??

if ($v =~ /\btest\b/) {
print "booya\n"
}


Then this is called pattern matching??

if ($v eq 'test') {
print "booya\n"
}


What is the difference between Pattern matching and Regular expressions???
 
In the expression "if ($v =~ /\btest\b/)", the part in
between the /'s is a regular expression.

"$v =~ /\btest\b/" is an instance of pattern-matching.
This can also be written "$v =~ m/\btest\b/", where the
"m" means "matches", but the "m" is considered implied if missing and is regularly omitted in Perl.

"if ($v eq 'test') {
print "booya\n"
}"

is NOT pattern matching. It's just a test for string
equality, no regular expression involved.

Pattern-matching in Perl is the process of checking to see
if a variable matches a regular expression. (That may not
be the most elegant possible definition, but it'll get you
by.)

 


In the expression "if ($v =~ /\btest\b/)", the part in
between the /'s is a regular expression.


What does the /'s represent?? I dont see it anywhere?
 
florida,

Like Mikevh's above post the /s is omited.
where the "s" means "search", but the "s" is considered implied if missing and is regularly omitted in Perl. I liked Mikevh way of putting it. Thanks Mikevh, hope you didn't mind.

It could have been written like so:
if ($v =~ s/\btest\b/)
 
Just to clarify. The match operator 'm//' is implied (not the 's//' operator which is the substitution operator).

The /'s are the delimiters. The /'s are usually used, but other delimiters can be used. Note that the 'm' must be used with other delimiters. Here's some examples.

The following 2 lines say: if the variable $v contains the string 'test'.
if ($v =~ /test/) # Match operator is implied by //
if ($v =~ m/test/)

What if you were searching for a date in the format mm/dd/yy? Using the '/' as a delimiter would require you to escape each '/' with a '\'. The following lines are equivalent.
if ($v =~ /12\/25\/2001/)
if ($v =~ m|12/25/2001|) # Use '|' as the delimiter - must explicitely use 'm'
 
Florida, when I said "/'s", I meant the SLASHES, which
delimit the regex.

Stiddy, I'm afraid "$v =~ s/\btest\b/" would make no sense, as there's nothing on the right-hand side of the regex to SUBSTITUTE, which is what the "s" stands for. It could be
written "$v =~ m/\btest\b/", which means "If $v MATCHES a
word boundary, the word 'test', and another word boundary."


 
Oh, thanks for pointing that out Mikevh. I got my '~ s/' and '~ tr/' mixed up. Glad you saw it for I would not want to point anyone in the wrong direction! [blush]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top