110 likes | 216 Views
Regular Expressions. What are Regular Expressions?. They are a way of specifying patterns Specifically patterns of symbols A set of rules A mini language A tiny, highly specialised programming language Number one use is for pattern matching – ie: finding things.
E N D
What are Regular Expressions? • They are a way of specifying patterns • Specifically patterns of symbols • A set of rules • A mini language • A tiny, highly specialised programming language • Number one use is for pattern matching – ie: finding things
Where could you use this awesome skill? • Python • Perl, C, Java, VB.NET, Javascript • Yahoo Widgets • Excel, Word ….
Basic RegEx Syntax [abc] = either a, b or c (class) a|b = a or b [a-z] = anything between a and z [^5] = anything except 5 . = anything (except a new line) * = multiple instances of the same char (0->) \ The backslash is a metacharacter (ab)* Round brackets group chars together . ^ $ * + ? { [ ] \ | ( )
Exercise 1 Which of the following matches regexp a(ab)*a • abababa • aaba • aabbaa • aba • aabababa
Exercise 2 • Which of the following matches regexp abc|xyz • abc • xyz • abc|xyz
Repetitions • a* = zero or more repetitions of a • a+ = one or more repetitions of a • a? = zero or one repetitions of a • (in other words, might not be there)
Exercise 3 • Which of the following matches regexp ab+c? • abc • ac • abbb • bbc
Exercise 4 • Which of the following matches regexp a.[bc]+ • abc • abbbbbbbb • azc • abcbcbcbc • ac • asccbbbbcbcccc