1 / 24

Regular Expressions

Regular Expressions. Regular Expression (or pattern) in Perl – is a template that either matches or doesn’t match a given string. Regular Expressions in Perl:. while( <STDIN> ){ if( /hello/ ){ … } }. if( $str =~ /hello/ ){ … }. @words = split /s+/ , $str;.

Télécharger la présentation

Regular Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Expressions Regular Expression (or pattern) in Perl – is a template that either matches or doesn’t match a given string. Regular Expressions in Perl: while( <STDIN> ){ if( /hello/ ){ … } } if( $str =~ /hello/){ … } @words = split /\s+/, $str;

  2. Regular Expressions (3) “.” matchs any char except a newline \n /hello.you/matches any string that has ‘hello’, followed by any one (exactly one) character, followed by ‘you’. /to*ols/ last character before ‘*’ may be repeated zero or more times. Matches ‘tools’,’tooooools’,’tols’ (but not ‘toxols’ !!!) /to+ols/ ------//------- one or more -----//------. /to.*ols/ matches ‘to’, followed by any string, followed by ‘ols’. /to?ols/ the character before ‘?’ is optional. Thus, there are only two matching strings – ‘tools’ and ‘tols’.

  3. Regular Expressions (4) Grouping – parentheses ‘( )’ are used for grouping one or more characters. /(tools)+/ matches “toolstoolstoolstools”. Alternatives: /hello (world|Perl)/ - matches “hello world”, “hello Perl”.

  4. Regular Expressions (5) Character Class /Hello [abcde]/ matches “Hello a” or “Hello b” … /Hello [a-e]/ the same as above Negating: [^abc] any char except a,b,c

  5. Regular Expressions (6) • Shortcuts • \d digit • \w word character [A-Za-z0-9_] • \s white space • Negative^ – [^\d] matches non digit • \S anything not \s • \D anything not \d

  6. Regular Expressions (8) /^abc/ - “^” beginning of a string /a\^bc/ - matches “\^” /[^abc]/ - negating Anchors ^ - marks the beginning of the string $ - marks the end of the string /^Hello Perl/ - matches “Hello Perl, good by Perl”, but not “Perl Hello Perl” /^\s*$/ - matches all blank lines

  7. Regular Expressions (9) \b - matches at either end of a word (matches the start or the end of a group of \w characters) /\bPerl\b/ - matches “Hello Perl”, “Perl” but not “Perl++” \B - negative of \b

  8. Regular Expressions (10) • Backreferences: • /(World|Perl) \1/ - matches “World World”, “Perl Perl”. • /((hello|hi) (world|Perl))/ • \1 refers to (hello|hi) (world|Perl) • \2 refers to (hello|hi) • \3 refers to (world|Perl) $1,$2,$3 store the values of \1,\2,\3 after a reg.expr. is applied.

  9. Examples: $str="Hello World"; if($str=~ /((Hello|Hi) (World|Perl))/){ print $1.":".$2.":".$3.":\n"; #output: #Hello World:Hello:World: } $str="Hello Perl Hi"; if($str=~ /((Hello|Hi) (World|Perl)) \1/){ print $1.":".$2.":".$3.":\n"; #output: non } $str="Hi Perl Hi Perl"; if($str=~ /((Hi|Hello) (World|Perl)) \1/){ print $1.":".$2.":".$3.":\n"; #output: #Hi Perl:Hi:Perl: } $date="12 10 10"; if($date=~ /(\d+)/){ print $1.":".$2.":".$3.":\n"; #output ($2 and $3 are empty): #12::: } if($date=~ /(\d+)(\s+\1)+/){ print $1.":".$2.":".$3.":\n"; #output (notice $3 is empty): #10: 10:: }

  10. Examples 1. What is it? /^0x[0-9a-fA-F]+$/ 2. Date format:Month-Day-Year -> Year:Day:Month $date = “12-31-1901”; $date =~ s/(\d+)-(\d+)-(\d+)/$3:$2:$1/;

  11. Examples 3. Make a pattern that matches any line of input that has the same word repeated two (or more) times in a row. Whitespace between words may differ.

  12. Example • /\w+/ #matches a word • /(\w+)/ #to remember later • /(\w+)\1/ #two times • /(\w+)\s+\1/ #whitespace between words • “This is a test” -> /\b(\w+)\s+\1/ • “This is thetheory” -> /\b(\w+)\s+\1\b/

  13. HomeWork • Write a regular expression that identifies a 24-hour clock. For example: 0:01, 00:20, 15:00, 23:59 • Write a regular expression that identifies a floating point. For example: 10, 10.0001, -0.1, +001.3456789 • For both assignments write a single program that identifies these patterns in the input lines and prints out only the matched patterns.

  14. HomeWork 3) Write a CGI Perl script that extracts all http links from a given WWW page. Input: http address. It is received from a HTML text box. Output: list of all http links found in <a href=“link”> field. Input Examples: http://www.tau.ac.il http://www.cs.tau.ac.il http://www.cnn.com

  15. HomeWork (3) Remarks: 1) You need to create two pages - (1) html page with a text box (2) cgi script that receives the input and formats output html file. 2) Unix command ‘wget’ downloads html files. 3) Use regular expressions. The code for parsing should be small, 3-10 lines.

  16. Regular Expressions Quantifiers: /a{3,6}/ - matches “a” repeated 3,4,5,6 times /(abc){3,}/ - matches three or more repetitions of “abc”. /a{3}/ - matches exactly three repetitions of “a”. * = {0,} + = {1,} ? = {0,1}

  17. Negated Match Negation if( $str =~ /hello/){ … } if( $str !~ /hello/){ … }

  18. Regular Expressions (11) $& - what really was matched $` - what was before $’ - the rest of the string after the matched pattern $` . $& . $’ - original string

  19. Regular Expressions (12) Substitutions: s/T/U/; #substitutes T with U (only once) s/T/U/g; #global substitution s/\s+/ /g; #collapses whitespaces s/(\w+) (\w+)/$2 $1/g; s/T/U/; #applied on $_ variable $str =~ s/T/U/;

  20. Regular Expressions (13) File Extension Renaming: my ($from, $to) = @ARGV; @files = glob (“*.$from”); foreach $file (@files){ $newfile = $file; $newfile =~ s/\.$from/\.$to/g; rename($file, $newfile); } =~ s/\.$from$/\.$to/g

  21. Split and Join $str=“aaa bbb ccc dddd”; @words = split /\s+/, $str; $str = join ‘:‘, @words; #result is “aaa:bbb:ccc:dddd” @words = split /\s+/, $_; “ aaa b” -> “”, “aaa”, “b” @words = split; “ aaa b” -> “aaa”, “b” @words = split ‘ ‘, $_; “ aaa b” -> “aaa”, “b”

  22. Grep grep EXPR, LIST; @results = grep /^>/, @array; @results = grep /^>/, <FILE>;

  23. regular expression  globes Regular Expressions (2) Regular Expressions in Unix: grep “include .*h” *.h

  24. Defined/Undef my %hash; #or %hash=(); defined %hash; #false, hash is empty $hash{“1”}=“one”; exists($hash{“1”})==defined($hash{“1”})==true; undef $hash{“1”}; exists($hash{“1”})== true; defined($hash{“1”})==false; delete $hash{“1”}; exists($hash{“1”})== false; defined($hash{“1”})==false; my $i; if( defined $i ) #false $i=0; if( defined $i ) #true

More Related