Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meaning of a regexp if ($_ =~ /-\n/)

Tags:

regex

perl

I am a beginner of perl scripting. I know hyphen (-) is used to specify the range.

But what if it is mentioned in the beginning of the expression?

Example:

if ($_ =~ /-\n/)
//do something

How to interpret the above code?

"if the parameter is equal to a range of newline" ? (No, that is weird understanding :-/)

Please help.

like image 761
SS Hegde Avatar asked Oct 11 '12 06:10

SS Hegde


People also ask

What does \+ mean in regex?

In posix-ere and other regex flavors, outside a character class ( [...] ), + acts as a quantifier meaning "one or more, but as many as possible, occurrences of the quantified pattern*. E.g. in javascript, s. replace(/\++/g, '-') will replace a string like ++++ with a single - .

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What does * represent in regex?

the * means "zero or more times" the parentheses group and capture, the \ allows you to match a special character (like the dot or the star)


1 Answers

Outside of [] - means "-" as far as I know, it only indicates a range within a [] block.

Here is a more complete answer I found

How to match hyphens with Regular Expression? (look at the second answer)

So the expression should match a - followed by a newline or line ending with -

like image 69
David Mårtensson Avatar answered Sep 24 '22 20:09

David Mårtensson