Reputation: 201
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
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
Upvotes: 0
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+$
Upvotes: 1