user1883361
user1883361

Reputation: 11

Regular expression which matches a specific pattern

I want to find a regular expression in Perl which matches a pattern such as this:

my $sumthing = "people say
for -->";

Over here after say there is a single newline character. So I need to find a regular expression which could match such a pattern which includes a newline within a pattern. Please help me to find this as I'm new to Perl & regular expression.

The possible methods I tried were these:

if (($sumthing !~ (/\n+$/)) && ($sumthing !~ (/^\n+/m)))

They kindly help me to find out an expression to match this kind of a pattern, but not getting the output as desired.

Upvotes: 1

Views: 87

Answers (3)

JRFerguson
JRFerguson

Reputation: 7516

Have a look at the /s modifier which causes .to match anything, including a newline.

my $str = "people say for\nsomething...";
$str =~ m{say(.*)}s and print "'$1'\n";

This would print:

' for
something...'

Upvotes: 0

raym0nd
raym0nd

Reputation: 3262

The following will check for anything then new line then anything. Not sure if I totally understood your question.

if($sumthing =~ m/.*\n.*/)

Upvotes: 0

ikegami
ikegami

Reputation: 385764

It's not clear what you want. Do you want match that string exactly? If so, you could use

$sumthing =~ /^people say\nfor -->\z/

or

$sumthing eq "people say\nfor -->"

Or maybe what you need to know is that . matches any character including newline when /s is used?

/people .* -->/s

Upvotes: 2

Related Questions