#!/usr/bin/perl

use 5.010;

# HOW PERL REGULAR EXPRESSIONS WORK
# Poem is "Neither Out Far Nor Deep In" by Robert Frost
# Format =  $[VARIABLE TO BE EXAMINED] =~ /[REGEX]/:

# Simple pattern:

	$L1 = "The people along the sand";
 		if ($L1 =~/people/){
		print $L1;
				}
	print "\n";

# You don't need to assign a variable, if you can examine last variable:

	$L2 = "All turn and look one way";

	$_ = $L2;
		if (/turn/){
			print $_;
			}
	print "\n";


# The Dot ("."), to represent a single character:

	$L3 = "They turn their back on the land";
	$L4 = "They look at the sea all day";	

		if ($L3 =~/b.ck/){
			print $L3;
				}

			print "\n";

		if ($L4 =~/l..k/){
			print $L4;
				}

			print "\n";
			print "\n";


# Character strings and numbers

	$L5 = "As long as it takes to pass 4700";
               if ($L5 =~/pas[a-zA-Z] [0-9]+/) {
		print "As long as it takes to pass";
			}

	print "\n";


# Shortcuts. \d+ is all numbers, \w+ is all letters, numbers & underspaces...

               if ($L5 =~/pas\w+ \d++/) {
		print "A ship keeps raising its hull";
			}

	print "\n";


# reg ex backspaces. "\1" stands in for the previous reg ex match...
# In this case, "/(t)\1/" matches an instance of the letter t being repeated.

	$_="The wetter ground like glass"; 

		if (/(t)\1/) { 
			print "The wetter ground like glass";
				}
	print "\n";


# backslash + # stands for paren expression. In this case the letter after "f" (l) is captured as \1...

	$_="Reflects a standing gull";
		if (/Ref(.)ects a standing gul\1/){
	print $_;
				}

	print "\n";
	print "\n";



# With Multiple parens each paren gets it's own post-\ number, incrementally

	$_="The land may vary more";
		if (/Th(.) l(.)nd m\2y v\2ry mor\1/){
			print $_;
			}

		print "\n";

# perl 5.10 also represents parens with \g{n} where n Is the number of the back ref.
# this can disambiguate tricky expressions. Negative numbers can also be used to represent
# relative placement

	$_="But wherever the truth may be";
		if (/Bu(.) wh(.)r\g{2}v\g{2}r \g{1}h\g{2} \g{1}ru\g{1}h may b\g{2}/){ 
			print $_;
			}
	
	print "\n";
	

# The vertical bar within parens can represent alternatives
	
	$_="The water comes ashore";

		if (/water|oil|sailors/) {
			print $_;
			}

	print "\n";

# You can use anchors to distinguish the start of a string("^"), the end of a string ("$") amd a 
# discrete word ("b")

	$_="And people look at the sea";

		if (/^And/) {
			if (/sea$/){
				if (/\blook\b/){
				print $_;
						}
					}
				}
			
	print "\n";
	print "\n";

