DOC PREVIEW
DREXEL CS 265 - Perl_Regular_Expressions_Part_2_m_k

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Perl Regular Expressions Part IIGroupingExtracting MatchesBackreferencesRegex Result VariablesQuantifying MetacharactersQuantifying Metacharacters continued…Greedy QuantifiersNon-Greedy QuantifiersMichael KovalchikCS 265, Fall 2009Parenthesis group parts of expressions together“/CS265|CS270/” => “/CS(265|270)/”Groups can be nested“/Perl|Pearl/” => “/P(e(a|))rl/”Parenthesis also serve to extract the matched stringsabcde matched with “/(abc)(de)/” will match the string and store ‘abc’ in the variable $1 and ‘de’ in $2Backreferences find earlier matches later in the string/1 is to match the group in $1, /2 in $2, etc.“/^(\w\w\w)\s\1$/” matches when a three letter word is duplicated with a space between the copiesThe results of a regular expression search are stored in special variables.◦$` (tilde) is set to the part of the string before a match◦$& is set to the part of the string that matched the expression◦$’ (single quote) is set to the part of the string after a matchUsing $`, $’ and to a lesser extent $& slows regular expression processing? – Matches the preceding 1 or 0 times◦a? = match the character ‘a’ 1 or 0 times. (‘a’ or ‘’)+ - Matches the preceding 1 or more times◦a+ = match ‘a’ atleast once. (‘a’ and ‘aaaa’)* - Matches the preceding 0 or more times◦a* = match ’a’ any number of times. (‘’, ‘a’, ‘aaaa’){n,m} – Matches atleast n, but less than m times.◦a{2,4} = match ‘a’ atleast 2, no more than 4 times. (‘aa’ and ‘aaaa’, but not ‘a’ or ‘aaaaa’){n,} – Match atleast n times.◦a{3,} = match ‘a’ atleast 3 times.(‘aaa’ and ‘aaaaa’, but not ‘a’ or ‘aa’){n} – match exactly n times.◦a{2} = match ‘a’ exactly 2 times. (‘aa’ only)Perl Quantifiers will match the longest string possible“the cat in the hat” =~ /^(.*)(cat)(.*)$/◦$1- ‘the ’◦$2- ‘cat’◦$3- ‘ in the hat’“the cat in the hat =~ /^(.*)(at)(.*)$/◦$1- ‘the cat in the h’◦$2- ‘at’◦$3- ‘’To have a quantifier match the smallest possible string append a ‘?’ to them.◦Examples: ??, *?, +? {n,m}?“the cat in the hat =~ /^(.*?)(at)(.*?)$/◦$1- ‘the c ’◦$2- ‘at’◦$3- ‘ in the


View Full Document

DREXEL CS 265 - Perl_Regular_Expressions_Part_2_m_k

Download Perl_Regular_Expressions_Part_2_m_k
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Perl_Regular_Expressions_Part_2_m_k and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Perl_Regular_Expressions_Part_2_m_k 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?