Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
^\s*(#*)\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([a-zA-Z0-9\.\- ]+)([^#]*#?\w*\s*)$
/
gmi

Description

This expression parses hosts file lines and extracts IP, domains, commented lines. Yet doesn't support ending comments.

Submitted by Jarek Wasilewski - 9 years ago