Home Perl Regex

Useful Patterns

 

    1. Single Quote
  my $SINGLE = qr{'(?:\\.|[^'\\])*'};

    2. Double Quote
  my $DOUBLE = qr{"(?:\\.|[^"\\])*"};

    3. Host Name
my @hosts = qw( 2aa.com aa2.to a..com
	ab..net a.com a2.com aa.234.com
	a.b.c.d_f.com ana.inter );

for ( @hosts )
{
	if ( $_ =~ m/^
	(
		(?! [-_0-9])	# first symbol is not -_[0-9] (optional)
		[\w]+ 		# first sybmol is letter!
	)
	(?>			# optimization
		\.[-\w]+	# one or more number of [-\w] after \.
	)*			# or nothing
	(?<=..)		  	# as minimum 2 symbols before!
		\.[a-z]{2,4}	# .com .net .ru .info etc.
	$/x

	&&

	$_ !~ m/(?>[-_.]{2,})/ ) # .-_ can not be together (optional)

	{ print "match: $_\n"; }
	else { print "not  : $_\n"; }
}
# not  : 2aa.com
# match: aa2.to
# not  : a..com
# not  : ab..net
# not  : a.com
# match: a2.com
# match: aa.234.com
# match: a.b.c.d_f.com
# not  : ana.inter