tags:

views:

268

answers:

7

Recently I downloaded a source (LevBot) and then I see this line:

} elsif($text =~ /^slaps $levbot_nick/i) {
  • But what /^ and /i do?
  • Why to use they?

I think this is regular expression, I'm right?

+3  A: 

Yes. See the perlre documentation. Briefly, /^ matches the start of a line, and /i means it's case-insensitive.

Brian Agnew
+3  A: 

Thats a regular expression

/^slaps $levbot_nick/i

/^ means starts with (actually ^ alone)

/i means ignore case (i alone after / /)

first and last / slashes are boundary of regex

S.Mark
+2  A: 

/^ beginning of the line
/i ignore size of the letters

hsz
+21  A: 
  • / Delimiter denoting start of regex (The char / is not part of the regex)
  • ^ Matches the start of a line
  • / Delimiter for the end of the regex (not part of the regex)
  • i Flag to make the regex case insensitive

Other flags possible are:

  • g Global
  • s Dot matches new line characters
  • x Extended - ignores whitespaces in the pattern and allows comments
  • m Multiline mode.
Amarghosh
Well, the / starts the match operator, not the regex. It's a slight but important difference.
brian d foy
I don't know Perl and I am not aware of the difference - can you please explain/give a link?
Amarghosh
The stuff inside the slashes is the actual "regular expression". The slashes merely indicate where the expression starts and stops, but are not a part of the actual expression.
Mark Canlas
@Amarghosh: the regular expression is the pattern that you're looking to match. The match operator is the syntax that tells the Perl interpreter: here comes a regex. In Perl, the match operator is normally delimited by '/' at start and end, but you can use delimiters (e.g., `m{^foo}`). See the section "Regexp Quote like operators" in perldoc perlop for the gory details. http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators
Telemachus
That was exactly my understanding - and that's exactly what I meant by `delimiter`
Amarghosh
Looking at Amarghosh's answer again, though, I'm not sure myself what brian objects to. Amarghosh didn't say that `/` was part of the regular expression; he said it denoted the start of the regex. That seems pretty reasonable to me here.
Telemachus
@Amarghosh: yeah, I reread your answer and came to that same conclusion. I think "delimiter denoting the start of the regex" is reasonable, but there may be a subtlety I'm missing as well.
Telemachus
@Telemachus Added clarification anyway.
Amarghosh
Amarghosh: gsx are not all flags that are allowed. 'e' and 'm' are important.
Alexandr Ciornii
i, m, s, x, and the mostly-useless o to control matching; c, e, and g to control substitution (hey, that's a C major chord!)
hobbs
+4  A: 

Yes, this is a regular expression.

  • The / on either side mark the pattern's beginning and end.
  • The ^ at the start of the patten means "only match this at the beginning of the string, and nowhere else."
  • The i after the end of the pattern is a modifier, it means "be case-insensitive when matching this", since the default is case-sensitive.
Adam Bellaire
+2  A: 

/^ matches the beginning of a line.

/i means case insensitive search.

Tim S. Van Haren
A: 

This syntax in Perl was inspired by awk's /xxx/ Pattern matching feature.

Jim Dennis