Download presentation
Presentation is loading. Please wait.
1
Nate Brunelle Today: Regular Expressions
CS1110 Nate Brunelle Today: Regular Expressions
2
Questions?
3
String.find() Takes a string as an argument, and if exactly that string appears, give its index Mystring.find(“Purple Elephant”) “purple elephant”.find(“Purple Elephant”) “the elephant was purple”
4
Wildcards [Rr]ugs?[^a-zA-Z] Match on/ find: Will not match on/find:
Rugged rugged We might want: A way of saying r or R å Maybe there’s an s ç Something that’s not a letter ê åugçê [Rr]ugs?[^a-zA-Z]
5
R string “\”” r“\”this” -> error r“\n” -> \n
6
Regex Pieces Operation Example Meaning Character class [Rr] or [rR]
[abcd] [\^] R or r Exactly one of a, b, c, or d Just carat (^) Character Range [a-z] [a-zA-Z] [0-9] Exactly one character “between” a and z “between” a and z or “between” A and Z Any one digit Negative character class [^a] [^a-zA-Z] [^\^] Any one character that’s not an a Any one character that’s not a letter any one character that’s not a carat Optional Quantifier s? [Rr]? Maybe there’s an s, 0 or 1 s Either have one of R or r or neither OR wx|xyz One of the strings wx or xyz Star [abc]* Any number of a’s b’s and c’s at all Plus [abc]+ At least one of a’s, b’s, and c’s
7
Regex Pieces, Cont. Operation Example Meaning
8
[Rr]? ‘’ R r (yz | xyz)?
9
Give an Expression to match
All UVA computing IDs 2-3 letters, number, 1-3 letters [a-z] [a-z] [a-z]?[1-9] [a-z] [a-z]? [a-z]?
10
Give strings which match:
[Dr]oc[^0-9][3-5!] rock! DocǓ4 [3-5!] A character (between 3 and 5) or !
11
Something we can’t do with [], *, +, ?
His or hers Hi?e?r?s His|hers H (i|er) s
12
Give strings which match:
fl[ou]*r? fl flour floor flu flo floo flououou flooooooooooooor floouuuouuuuuuuuuo flur flr Fl[ou]*r Flr | fl[ou]r | fl[ou][ou]r …
13
Give an Expression to match
3 words separated by spaces [a-z]* [a-z]* [a-z]* The box [a-z]+ [a-z]+ [a-z]+
14
Give an Expression to match
nate and brunelle within 3 words of one another nate man myth legend brunelle nate the rock brunelle Nate brunelle nate ([a-z]+ )?([a-z]+ )?([a-z]+ )?brunelle
15
In python import re Compile Operate Match Object search finditer
Similar to string.find() finditer Findall 0 parentheses: m.group() 1 paren: m.group(1) 2+ paren: m.groups() Match Object group start end groups
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.