Vasanth
Vasanth

Reputation: 201

Particular line match in Regex Perl

Input:

gis.google.com
CAT
DOG
gis1.goole.com
CAT gis.google.com
gis.google.com DOG

I have used below regex to match the prticlar pattern per line.

/^[\w]+\s+[\w]+[\.][\w]+[\.][\w]+$/mg

It is matching below

DOG
gis1.goole.com
CAT gis.google.com

But I wanted to match only one line

   CAT gis.google.com

Please help me to sort it out. Thanks in advance!

Upvotes: 0

Views: 377

Answers (2)

Miller
Miller

Reputation: 35208

How are you using the regex?

It could be simplified a little, but it works when done using line by line processing:

while (<DATA>) {
    print if /^\w+\s+\w+[.]\w+[.]\w+$/;
}

__DATA__
gis.google.com
CAT
DOG
gis1.goole.com
CAT gis.google.com
gis.google.com DOG

Outputs:

CAT gis.google.com

Live Demo

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174834

Replace \s+ with \h+ where \s would match spaces plus newline characters also but \h only matches the horizontal spaces. That's the reason why the consecutive two lines (DoG and it's following line) got matched. And also remove all the unnecessary character classes from your regex.

^\w+\h+\w+\.\w+\.\w+$

DEMO

Upvotes: 1

Related Questions