regular expressions

Post on 16-May-2015

135 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Regular Expressions 101@!#?@!

KNOW YOUR ENEMY

rubular.com

Part ICharacter Literals and Wildcards

"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

"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

"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

"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

"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

"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

"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

/[A-z]/?

"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

"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

"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

"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

/\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

"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

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.

Part IIQuantifiers

"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

"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

"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

"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

"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

"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

"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

squee squeee squeeee squeeeee squeeeeee!

/sque{3,4}/

http://tinyurl.com/rx101-26

squee squeee squeeee squeeeee squeeeeee!

/sque{4,}/

http://tinyurl.com/rx101-27

squee squeee squeeee squeeeee squeeeeee!

/sque{,2}/

http://tinyurl.com/rx101-28

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

“TMTOWTDI”

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

“TMTOWTDI”

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

“TMTOWTDI”

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 stanley@aol.com - you will not be disappoint.

http://tinyurl.com/rx101-a2

Part IIIAnchors

"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

"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

"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

"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

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

http://tinyurl.com/rx101-24

Part IVMatch Groups

"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

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

"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

"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

Walkthrough!

http://tinyurl.com/rx101-a3

Part VFlags

/cat/giflags

regex

delimiters

Perl / PCRE Style

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

flagsregex

delimiters

Java Style

Javaglue

i CASE_INSENSITIVE

g

s DOTALL

m MULTILINE

Part VIHow do I use my powers?

grepaka cheat at Letterpress!

Your Text Editor!

$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

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

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

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

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) );

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.

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) );

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

top related