regular expressions

62
Regular Expressions 101 @!#?@!

Upload: pawel-szymczykowski

Post on 16-May-2015

135 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Regular expressions

Regular Expressions 101@!#?@!

Page 2: Regular expressions

KNOW YOUR ENEMY

Page 3: Regular expressions
Page 4: Regular expressions

rubular.com

Page 5: Regular expressions

Part ICharacter Literals and Wildcards

Page 6: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/m/

http://tinyurl.com/rx101-2

Page 7: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/me/

http://tinyurl.com/rx101-4

Page 8: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/./

http://tinyurl.com/rx101-5

Page 9: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/\./

http://tinyurl.com/rx101-8

Page 10: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/.e/

http://tinyurl.com/rx101-10

Page 11: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/[aeiou]/

http://tinyurl.com/rx101-11

Page 12: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/[a-f]/

http://tinyurl.com/rx101-12

Page 13: Regular expressions

/[A-z]/?

Page 14: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/[a-fw-z]/

http://tinyurl.com/rx101-13

Page 15: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/[^A-Z]/

http://tinyurl.com/rx101-14

Page 16: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/\d/

http://tinyurl.com/rx101-15

Page 17: Regular expressions

"Now_is_the_time_for_all_good_men_to_come_to_the_aid_of_their_country."_was_proposed_as_a_typing_drill_by_a_teacher_named_Charles_E._Weller_aka_"Chase"_in_1918.

/\s/

http://tinyurl.com/rx101-16

Page 18: Regular expressions

/\w/

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

http://tinyurl.com/rx101-17

Page 19: Regular expressions

"Now_is_the_time_for_all_good_men_to_come_to_the_aid_of_their_country."_was_proposed_as_a_typing_drill_by_a_teacher_named_Charles_E._Weller_aka_"Chase"_in_1918.

/\W/

http://tinyurl.com/rx101-18

Page 20: Regular expressions

Activity!Write a regular expression to match the telephone number in this text:

http://tinyurl.com/rx101-a1

For a good time call Stanley: 555-1212 - you will not be disappoint.

Page 21: Regular expressions

Part IIQuantifiers

Page 22: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/men?/

http://tinyurl.com/rx101-19

Page 23: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/".*"/

http://tinyurl.com/rx101-20

Page 24: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/".*?"/

http://tinyurl.com/rx101-21

Page 25: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/"[^"]*"/“TMTOWTDI”

http://tinyurl.com/rx101-22

Page 26: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/t\w+[aeiou]/

http://tinyurl.com/rx101-23

Page 27: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/t\w*[aeiou]/

http://tinyurl.com/rx101-24

Page 28: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/[aeiou]{2}/

http://tinyurl.com/rx101-25

Page 29: Regular expressions

squee squeee squeeee squeeeee squeeeeee!

/sque{3,4}/

http://tinyurl.com/rx101-26

Page 30: Regular expressions

squee squeee squeeee squeeeee squeeeeee!

/sque{4,}/

http://tinyurl.com/rx101-27

Page 31: Regular expressions

squee squeee squeeee squeeeee squeeeeee!

/sque{,2}/

http://tinyurl.com/rx101-28

Page 32: Regular expressions

/men?/ /men{0,1}/=

“TMTOWTDI”

Page 33: Regular expressions

/.*/ /.{0,}/=

“TMTOWTDI”

Page 34: Regular expressions

/.+/ /.{1,}/=

“TMTOWTDI”

Page 35: Regular expressions

Activity!Rewrite the telephone number regular expression

to use quantifiers.

http://tinyurl.com/rx101-a1

For a good time call Stanley: 555-1212 - you will not be disappoint.

Extra credit - extract the email address.

For a good time email [email protected] - you will not be disappoint.

http://tinyurl.com/rx101-a2

Page 36: Regular expressions

Part IIIAnchors

Page 37: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/^[^\s]+/

http://tinyurl.com/rx101-29

Page 38: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/[^\s]+$/

http://tinyurl.com/rx101-30

Page 39: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/\ba\w*/

http://tinyurl.com/rx101-31

Page 40: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/\b\w{3}\b/

http://tinyurl.com/rx101-32

Page 41: Regular expressions

Activity!Go back and fix this example to find wholewords that start with ‘t’ and end in a vowel.

http://tinyurl.com/rx101-24

Page 42: Regular expressions

Part IVMatch Groups

Page 43: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/"(.*?)"/

1. Now is the time for all good men to come to the aid of their country.2. Chase

http://tinyurl.com/rx101-33

Page 44: Regular expressions

str = '"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.'puts str.gsub(/"(.*?)"/, '<i>\1</i>')

<i>Now is the time for all good men to come to the aid of their country.</i> was proposed as a typing drill by a teacher named Charles E. Weller aka <i>Chase</i> in 1918.

Replacement

Page 45: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/\b(the|is|of)\b/

1. is2. the3. the4. of

http://tinyurl.com/rx101-34

Page 46: Regular expressions

"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.

/\b(?:the|is|of)\b/

http://tinyurl.com/rx101-35

Page 47: Regular expressions

Walkthrough!

http://tinyurl.com/rx101-a3

Page 48: Regular expressions

Part VFlags

Page 49: Regular expressions

/cat/giflags

regex

delimiters

Perl / PCRE Style

Page 50: Regular expressions

Pattern.compile(“cat”, CASE_INSENSITIVE | DOTALL);

flagsregex

delimiters

Java Style

Javaglue

Page 51: Regular expressions

i CASE_INSENSITIVE

g

s DOTALL

m MULTILINE

Page 52: Regular expressions

Part VIHow do I use my powers?

Page 53: Regular expressions

grepaka cheat at Letterpress!

Page 54: Regular expressions

Your Text Editor!

Page 55: Regular expressions

$str = 'three cats chased five mice';$str =~ /(cats|mice)/;print $1;$str =~ s/(cats|mice)/fuzzy $1/g;print $str;

catsthree fuzzy cats chased five fuzzy mice

Page 56: Regular expressions

var str = 'three cats chased five mice';var matches = str.match(/(cats|mice)/);console.log( matches[1] );str.replace(/(cats|mice)/g, ‘fuzzy $1’);console.log( str );

catsthree fuzzy cats chased five fuzzy mice

Script

Page 57: Regular expressions

str = 'three cats chased five mice'matches = str.match(/(cats|mice)/)puts matches[1]str.gsub!(/(cats|mice)/, ‘fuzzy \1’)puts str

catsthree fuzzy cats chased five fuzzy mice

Page 58: Regular expressions

String str = “three cats chased five mice”;Pattern p = Pattern.compile(“(cats|mice)”);Matcher m = p.matcher( str );System.out.println( m.group(1) );String newStr = str.replaceAll(“(cats|mice)”, “fuzzy $1”);System.out.println( str );

catsthree fuzzy cats chased five fuzzy mice

Page 59: Regular expressions

String str = “three cats chased five mice”;Pattern p = Pattern.compile(“\b\w{4}\b”);Matcher m = p.matcher( str );System.out.println( m.group(1) );

Page 60: Regular expressions

String str = “three cats chased five mice”;Pattern p = Pattern.compile(“\b\w{4}\b”);Matcher m = p.matcher( str );System.out.println( m.group(1) );

No.

Page 61: Regular expressions

String str = “three cats chased five mice”;Pattern p = Pattern.compile(“\\b\\w{4}\\b”);Matcher m = p.matcher( str );System.out.println( m.group(1) );

Page 62: Regular expressions

http://www.regexplanet.com/advanced/java/index.html