43

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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Invictus
  • 2,653
  • 8
  • 31
  • 50
  • 4
    If you want to spend a few minutes learning about regular expressions in Perl have a look at [perlrequick](http://perldoc.perl.org/perlrequick.html). – Sebastian Stumpf Apr 04 '12 at 21:00
  • 1
    _Learning Perl_ explains it all :) – brian d foy Apr 04 '12 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 06 '20 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 01 '20 at 21:11

4 Answers4

53

=~ 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".

simbabque
  • 53,749
  • 8
  • 73
  • 136
jmhl
  • 1,645
  • 12
  • 11
  • 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 04 '12 at 21:07
  • 2
    No, `m//` is the operationoperator testing the regex. `=~` simply tells `m//` (and `s///` and `tr//`) which variable to test against. – ikegami Apr 04 '12 at 21:53
  • 2
    No, `/9eaf/` is not the regular expression. `/9eaf/` is the match operator. `9eaf` is the regular expression. – ikegami Apr 04 '12 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.

9

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
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.

  • 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 04 '12 at 21:09
  • Why don't you try it for yourself? –  Apr 04 '12 at 21:38