kelotich_
kelotich_

Reputation: 11

Regular expression in perl does not work as expected

I have a simple bash script that uses a line of perl code + regex to extract the necessary piece of string. It looks like

ANSWER=$(host $IPW 2>/dev/null | perl -p -e 's#^.+\s\b([a-zA-Z]{4,8}\d{1,3})(?=-\d\.).+$#\1#;'

It works for the most part, but produces unexpected matches from time to time. Example:

$ echo "Host 31.201.188.199.in-addr.arpa. not found: 3(NXDOMAIN)" | perl -p -e 's#^.+?\s\b([a-zA-Z]{4,8}\d{1,3})(?=-\d\.).+?(?=\.$)#\1#;'

Host 31.201.188.199.in-addr.arpa. not found: 3(NXDOMAIN)

The string is supposed to match parts of string like "server100" (letters + digits) and return the corresponding part. Is there something I am missing or don't understand yet. (sorry for bothering)

Upvotes: 0

Views: 100

Answers (2)

Borodin
Borodin

Reputation: 126722

I assume the sample text that you show shouldn't be printed at all?

I suggest that you use a simple match instead of a substitution. I've also removed the superfluous parts of your regex pattern

perl -lne 'print $1 if /.*\s([a-z]{4,8}\d{1,3})(?=-\d\.)/i'

Upvotes: 2

that other guy
that other guy

Reputation: 123400

Your regex doesn't match, so no substitution is made. The line is therefore printed as is.

If you don't want to print when there is no match, you can use -n instead of -p, plus and print to print the line on successful substitution:

echo "Host 31.201.188.199.in-addr.arpa. not found: 3(NXDOMAIN)" | 
    perl -n -e 's#^.+?\s\b([a-zA-Z]{4,8}\d{1,3})(?=-\d\.).+?(?=\.$)#\1# and print'

Upvotes: 3

Related Questions