48

I guess the tag is a variable, and it is checking for 9eaf - but does this exist in Perl?

What is the "=~" sign doing here and what are the "/" characters before and after 9eaf doing?

if ($tag =~ /9eaf/)
{
    # Do something
}
4
  • 5
    If you want to spend a few minutes learning about regular expressions in Perl have a look at perlrequick. Apr 4, 2012 at 21:00
  • 1
    Learning Perl explains it all :) Apr 4, 2012 at 23:38
  • 5
    This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. Quite the opposite.
    – jinawee
    Feb 6, 2020 at 12:06
  • man perlop "perlop - Perl operators and precedence" (at least in UNIX-like) answers this question: "Binary "=~" binds a scalar expression to a pattern match."
    – U. Windl
    Apr 1, 2020 at 21:11

4 Answers 4

55

=~ is the operator testing a regular expression match. The expression /9eaf/ is a regular expression (the slashes // are delimiters, the 9eaf is the actual regular expression). In words, the test is saying "If the variable $tag matches the regular expression /9eaf/ ..." and this match occurs if the string stored in $tag contains those characters 9eaf consecutively, in order, at any point. So this will be true for the strings

9eaf

xyz9eaf

9eafxyz

xyz9eafxyz

and many others, but not the strings

9eaxxx
9xexaxfx

and many others. Look up the 'perlre' man page for more information on regular expressions, or google "perl regular expression".

3
  • You mean if there is a space in between it does not work... for example... 9sssbt yyuuiihh 88880099 9eaf 888hhjjj nnmmmm. Will the above logic for this string???
    – Invictus
    Apr 4, 2012 at 21:07
  • 2
    No, m// is the <strike>operation</strike>operator testing the regex. =~ simply tells m// (and s/// and tr//) which variable to test against.
    – ikegami
    Apr 4, 2012 at 21:53
  • 2
    No, /9eaf/ is not the regular expression. /9eaf/ is the match operator. 9eaf is the regular expression.
    – ikegami
    Apr 4, 2012 at 21:54
13

The '=~' operator is a binary binding operator that indicates the following operation will search or modify the scalar on the left.

The default (unspecified) operator is 'm' for match.

The matching operator has a pair of characters that designate where the regular expression begins and ends. Most commonly, this is '//'.

Give Perl Re tutorial a read.

10

The code is testing whether 9eaf is a substring of the value of $tag.


$tag =~ /9eaf/

is short for

$tag =~ m/9eaf/

where m// is the match operator. It matches the regular expression pattern (regexp) 9eaf against the value bound by =~ (returned by the left hand side of =~).


Operators, including m// and =~, are documented in perlop.

Regular expressions (e.g. 9eaf) are documented in perlre, perlretut.

3

That checks for a match of the scalar $tag (which is presumably a string) against the regular expression /9eaf/, which merely checks to see if the string "9eaf" is a substring of $tag. Check out perldoc perlretut.

2
  • You mean if there is a space in between it does not work... for example... 9sssbt yyuuiihh 88880099 9eaf 888hhjjj nnmmmm. Will the above logic for this string???
    – Invictus
    Apr 4, 2012 at 21:09
  • Why don't you try it for yourself?
    – user554546
    Apr 4, 2012 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.