5

I thought getting an IP address on OSX or Linux would be rather easy to learn how to use regular expression with grep, but it looks like I either have a syntax error or misunderstanding of what is required.

I know this regex is correct, although I know it may not be a valid IP address I'm not considering that at this point.

(\d{1,3}\.){3}\d{1,3}

so I'm not sure why this doesn't work.

ifconfig | grep -e "(\d{1,3}\.){3}\d{1,3}"
4

2 に答える 2

0

\dは grep では認識されません。代わりに[0-9]orを使用できます[[:digit:]]。残念ながら、正規表現には多くの方言があります。{、、およびもエスケープする必要が}あります。以下は私のために働く()

/sbin/ifconfig | grep -e "\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}"
于 2013-03-29T23:42:16.843 に答える