Nathan Campos
Nathan Campos

Reputation: 29507

What is /^ and /i in Perl?

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

} elsif($text =~ /^slaps $levbot_nick/i) {

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

Upvotes: 1

Views: 15280

Answers (8)

Nicky
Nicky

Reputation: 1

  1. ^(carat) symbol denotes regex 'starting with'. Here the pattern should start with slaps.

    /i denotes case insensitive search

  2. To match pattern

Yes, you are right. It is regular expression.

In your case matching pattern is starting with slaps along with value in levbot_nick variable.

Upvotes: 0

Jim Dennis
Jim Dennis

Reputation: 17510

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

Upvotes: 0

Amarghosh
Amarghosh

Reputation: 59461

  • / 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.

Upvotes: 26

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

/^ matches the beginning of a line.

/i means case insensitive search.

Upvotes: 3

Adam Bellaire
Adam Bellaire

Reputation: 110499

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.

Upvotes: 4

hsz
hsz

Reputation: 152266

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

Upvotes: 2

YOU
YOU

Reputation: 123881

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

Upvotes: 3

Brian Agnew
Brian Agnew

Reputation: 272347

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

Upvotes: 3

Related Questions