Download presentation
Presentation is loading. Please wait.
Published byAugust Haynes Modified over 9 years ago
1
Regular Expression Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn
2
Dr. Tran, Van Hoai 2007 Regular Expression Text pattern Used for text-processing utilities Text-pattern = normal characters + metacharacters = regular expression Metacharaters in regular expressions are different from those in file name expansion
3
Dr. Tran, Van Hoai 2007 Regular Expression Example (1) grep [A-Z]* script*.sh means grep a.txt abc script1.sh script2.sh grep "[a-z]*" script*.sh means to find the pattern " [a-z]* " in " script*.sh " Good and safe solutions are "" and ''
4
Dr. Tran, Van Hoai 2007 Regular Expression Metacharacter sets Depends on usage context searching replacing Also depends on programs Different engines Perl PHP .NET regular expression library Java JDK
5
Dr. Tran, Van Hoai 2007 Regular Expression Searching patterns (1) CharacterPattern. single character, except newline * any number of characters immediately preceding it ^ the following regex at the beginning [ ] any one of the enclosed characters, which can be given in range (-). "^" right after "[" means not {n,m}{n,m} a range of occurences of regex preceding it. {n} matches exactly n occurrences. {n,} matches at least n occurrences. {n,m} matches occurrences between n and m \ turn off special meaning \b word boundary
6
Dr. Tran, Van Hoai 2007 Regular Expression Searching patterns (2) CharacterPattern \{n,m\} a range of occurences of regex preceding it. {n} matches exactly n occurrences. {n,} matches at least n occurrences. {n,m} matches occurrences between n and m + one or more instances of preceding regex ? zero or one instances of preceding regex | alternation ( ) apply match to an enclosed group of regex
7
Dr. Tran, Van Hoai 2007 Regular Expression Example (2) PatternWhat does it match? bag ^bagbag at the beginning of line bag$bag at the end of line ^bag$ only bag on the line [Bb]agBag or bag
8
Dr. Tran, Van Hoai 2007 Regular Expression Example (3) PatternWhat does it match? b[aeiou]g second letter is vowel b[^aeiou]g second letter is consonant (or uppercase, symbol) b.g second letter is any ^\. any line begins with a dot ^\.[a-z][a-z] ? ^[^\.] ? bugs*bug, bugs, bugss,etc.
9
Dr. Tran, Van Hoai 2007 Regular Expression Example (4) PatternWhat does it match? "word" ? "*word"* ? [A-Z][A-Z]* one or more uppercase letters ^\. any line begins with a dot ^\.[a-z][a-z] ? ^[^\.] ? bugs*bug, bugs, bugss,etc.
10
Dr. Tran, Van Hoai 2007 Regular Expression Example (5) PatternWhat does it match? floating point number java identifier java simple arithmetic expression
11
Dr. Tran, Van Hoai 2007 Regular Expression Replacing patterns (1) CharacterPattern \ turn off special meaning \n reuse the text matched by the nth subpattern previously saved by \( and \). Numbered from 1 to 9 & text match search pattern ~, % reuse previous replacement pattern \u convert first character of replacement pattern to uppercase \U convert entire replacement pattern to uppercase \l, \L same; to lowercase \e turn off previous \u or \l \E turn off previous \U or \L
12
Dr. Tran, Van Hoai 2007 Regular Expression Example (6) CommandResult s/.*/( & )/ add space and parentheses s/.*/mv & &.old/ ? /^$/d delete blank lines (vi, g/^$/d for all lines) %s/ */ /g turn one or more spaces into one space %s/.*/\L&/ lowercase entire file %s/yes/No/g replace yes to No %s/Yes/~/g replace yes to No (previous replacement) s/\(F\)\(ORTRAN\ )/\1\L\2/g FORTRAN to Fortran
13
Dr. Tran, Van Hoai 2007 Regular Expression Applications PatternMatched text grab a specific HTML tag [0-9]\{1,3\}\. ???? IP address Email address Valid dates (day-month-year) WeWe, does not match Wee ]*>\(.*?\) [A-Z0-9._%-]+@[A-Z0-9.- ]+\.[A-Z]{2,4}
14
Dr. Tran, Van Hoai 2007 Regular Expression text processing utilities is NEXT
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.