Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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}

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

3 We want to match ‘hello’ not followed by ‘perl’ and followed by ‘!’ hello world! but not: hello perl! Negated Match (2) /(hello)\s+(?!perl)\w+!/ Extended Regular Expression: (?expression) http://www.perldoc.com/perl5.6/pod/perlre.html Reminder: we’ve seen only /[^abc]/

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

5 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/;

6 Reg-Expr vs. Globes Regular Expressions in Unix: grep “include.*h” *.h regular expression globes 

7 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

8 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”

9 Grep grep EXPR, LIST; @results = grep /^>/, @array; @results = grep /^>/, ;

10 Defined/Undef my $i; if( defined $i ) #false $i=0; if( defined $i ) #true 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;


Download ppt "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."

Similar presentations


Ads by Google