1 / 28

Regular Expressions

Regular Expressions. A Gentle Introduction Rani Nelken * Chinese examples and reference prepared by Chen Shih-Pei. Motivation. Powerful language for specifying text patterns Search, substitute, split, etc. jEdit support. Running example. "Exuberance is Beauty." --William Blake

rey
Download Presentation

Regular Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Expressions A Gentle Introduction Rani Nelken *Chinese examples and reference prepared by Chen Shih-Pei

  2. Motivation • Powerful language for specifying text patterns • Search, substitute, split, etc. • jEdit support

  3. Running example "Exuberance is Beauty." --William Blake 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。 --明史列傳

  4. Simple match "Exuberance is Beauty." --William Blake be 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。 --明史列傳 字

  5. Period "Exuberance is Beauty." --William Blake . 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。 --明史列傳 . => Period matches any single character

  6. Repeating Things "Exuberance is Beauty." --William Blake .* .+ * means repeat zero or more times + means repeat one or more times

  7. Repeating Things 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。--明史列傳 .* .+ * means repeat zero or more times + means repeat one or more times

  8. Repeating Things "Exuberance is Beauty." --William Blake l+ "Exuberance is Beauty." --William Blake b+

  9. Repeating Things 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。--明史列傳 -+ 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。--明史列傳 ,+

  10. Repeating Things "Exuberance is Beauty." --William Blake ut+ "Exuberance is Beauty." --William Blake ut*

  11. Repeating Things 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。--明史列傳 知縣+ 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。--明史列傳 知州*

  12. Counting Repetitions "Exuberance is Beauty." --William Blake ut? "Exuberance is Beauty." --William Blake l{2} l{2,3}

  13. Counting Repetitions 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。--明史列傳 知州? 魏良弼,字師說,新建人。嘉靖二年進士。授松陽知縣,召拜刑科給事中。--明史列傳 -{2} -{2,3}

  14. Character classes "Exuberance is Beauty." --William Blake \w "Exuberance is Beauty." --William Blake \W "Exuberanceis Beauty." --William Blake \s "Exuberanceis Beauty." --William Blake \S

  15. Character classes 魏良弼,字師說,新建人。嘉靖二年進士。 \w 魏良弼,字師說,新建人。嘉靖二年進士。 \W 魏良弼,字師說,新建人。嘉靖二年進士。 \s (no match) 魏良弼,字師說,新建人。嘉靖二年進士。 \S *Note: \w is a more vague class for Chinese characters

  16. Character classes "Exuberance is 123." \d [0-9] "Exuberance is 123." \d+ [1-3]+ "Exuberance is 123." [x-y] : character range

  17. Character classes Define your own character classes: 魏良弼,字師說,新建人。嘉靖二年進士。 [一二三四五六七八九十] For consecutive Chinese numbers: 正德十二年進士。 [一二三四五六七八九十]+

  18. Greedy and Ungreedy Matching "Exuberance is Beauty." --William Blake .+b "Exuberance is Beauty." --William Blake .+B "Exuberance is Beauty." --William Blake .+?B

  19. Greedy and Ungreedy Matching "Exuberance is Beauty." --William Blake .+b "Exuberance is Beauty." --William Blake .+B "Exuberance is Beauty." --William Blake .+?B

  20. Greedy and Ungreedy Matching 宣宗孝恭皇后孫氏,鄒平人。幼有美色。父忠,永城縣主簿也。誠孝皇后母彭城伯夫人,故永城人,時時入禁中 ,.+人 宣宗孝恭皇后孫氏,鄒平人。幼有美色。父忠,永城縣主簿也。誠孝皇后母彭城伯夫人,故永城人,時時入禁中 ,.+?人

  21. Notes on writing your own regex • Multiple styles: the one we’re using is Perl-like • Be careful and precise on what you write: you might match more patterns than you want

  22. Regular Expression Syntax Reference (Perl-Style)

  23. Characters

  24. Character Classes

  25. Predefined Character Classes

  26. Greedy quantifiers Greedy matching (default): is to match with the longest possible string of characters that matches the pattern

  27. Reluctant quantifiers Reluctant matching: to force the expression to find the shortest rather than the longest match

  28. Logical operators • Back references

More Related