0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

Python & Perl: Tight & Loose Regular Expression Matching

352

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
352
On Slideshare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript

• 1. Python & Perl Tight & Loose Regular Expressions in Perl Vladimir Kulyukinwww.youtube.com/vkedco www.vkedco.blogspot.com
• 2. Outline ● Loose Regular Expressions ● Tight Regular Expressions ● Source code is can be downloaded from herewww.youtube.com/vkedco www.vkedco.blogspot.com
• 3. Problem ● We will use the following problem to contextualize our study of loose and tight regular expressions Design regular expressions to recognize these two languages L1 and L2: – L1 = {(ab)^n | n >= 1} – L2 = {a^n | n is even} U {b^n | n is odd}www.youtube.com/vkedco www.vkedco.blogspot.com
• 4. Languages L1 and L2 ● L1 is an infinite of strings each of which is a non-zero concatenation of the string ab with itself ● In other words, L1 = {ab, abab, ababab, …} ● L2 is the union of two infinite sets S1 and S2: – S1 is the set of even-length strings of as; S1 = {, aa, aaaa, aaaaaa, ...} – S2 is the set of odd-length strings of bs; S2 = {b, bbb, bbbbb, bbbbbbb, …} ● A string s is in L2 if and only if s is either in S1 or in S2; this is an exclusive or because the intersection of S1 and S2 is emptywww.youtube.com/vkedco www.vkedco.blogspot.com
• 5. Testing Regular Expressions ## We will use this subroutine to test regular expressions sub test_regex {    ## Take a regular expression and a list of strings    my (\$regex, \$list) = @_;    print "nMatching \$regexn";    foreach (@{\$list}) {      ## Match the regular expression with each string in the list      print match for , "\$_", "n" if \$_ =~ /\$regex/;    }        print "n"; }www.youtube.com/vkedco www.vkedco.blogspot.com
• 6. Regex 01 for L1 ##  a  match  is  successful  so  long  ## as the string with which  ## \$re_01_L1 is bound with =~  ## contains a sub­string  ## that matches \$re_01_L1.  my \$re_01_L1 = (ab)+;www.youtube.com/vkedco www.vkedco.blogspot.com
• 7. Whats the Output? my @list = (, ab, abab, ababab,  abbb, aaaa, aaa, aaaaaa, b,  bbb, bbbbb, abbaabba); my \$re_01_L01 = (ab)+; test_regex(\$re_01_L01, @list);www.youtube.com/vkedco www.vkedco.blogspot.com
• 8. Output for Regex 01 for L1 match for ab match for abab match for ababab match for abbb match for abbaabbawww.youtube.com/vkedco www.vkedco.blogspot.com
• 9. Regex 02 for L1 ## To tighten our matches, we can use the ## special characters ^ and \$ in our ## regular expression specification so ## that it is both left justified and ## right justified. This guarantees ## the matches only with those strings ## that contain only the non-zero number ## of occurrences of ab and nothing else. my \$re_02_L01 = ^(ab)+\$;www.youtube.com/vkedco www.vkedco.blogspot.com
• 10. Whats the Output? my @list = (, ab, abab, ababab,  abbb, aaaa, aaa, aaaaaa, b,  bbb, bbbbb, abbaabba); my \$re_02_L01 = ^(ab)+\$; test_regex(\$re_02_L01, @list);www.youtube.com/vkedco www.vkedco.blogspot.com
• 11. Output for Regex 02 for L1 match for ab match for abab match for abababwww.youtube.com/vkedco www.vkedco.blogspot.com
• 12. Regex 01 for L2 ## \$re_01_L02, is loose in that  ## it matches a string so long  ## as it contains a matching  ## substring.  my \$re_01_l2 = ((aa)*|b(bb)*);www.youtube.com/vkedco www.vkedco.blogspot.com
• 13. Whats the Output? my @list = (, ab, abab, ababab,  abbb, aaaa, aaa, aaaaaa, b,  bbb, bbbbb, abbaabba); my \$re_01_L2 = ((aa)*|b(bb)*); test_regex(\$re_01_L2, @list);www.youtube.com/vkedco www.vkedco.blogspot.com
• 14. Output for Regex 01 for L2 match for  match for ab match for abab match for ababab match for abbb match for aaaa match for aaa match for aaaaaa match for b match for bbb match for bbbbb match for abbaabbawww.youtube.com/vkedco www.vkedco.blogspot.com
• 15. Regex 02 for L2 ## \$re_02_L02 is tight in that  ## it matches a string if and only ## if it is contains an even num­ ## ber of as or an odd number  ## of bs and nothing else. my \$re_02_L2 = ^((aa)*|b(bb)*)\$;www.youtube.com/vkedco www.vkedco.blogspot.com
• 16. Whats the Output? my @list = (, ab, abab, ababab,  abbb, aaaa, aaa, aaaaaa, b,  bbb, bbbbb, abbaabba); my \$re_02_L02 = ^((aa)*|b(bb)*)\$; test_regex(\$re_02_L02, @list);www.youtube.com/vkedco www.vkedco.blogspot.com
• 17. Output for Regex 01 for L2 match for  match for aaaa match for aaaaaa match for b match for bbb match for bbbbbwww.youtube.com/vkedco www.vkedco.blogspot.com
• 18. References ● www.python.org ● http://docs.python.org/2/ ● www.perl.org ● http://perldoc.perl.org/www.youtube.com/vkedco www.vkedco.blogspot.com