110 likes | 251 Views
Why? (Wh Why). Regular expressions provide a means of identifying if a string of characters is allowed in a particular language . Regular expressions are used by many text editors, e.g. to find matches to searches.
E N D
Why? (Wh Why) • Regular expressions provide a means of identifying if a string of characters is allowed in a particular language. • Regular expressions are used by many text editors, e.g. to find matches to searches. • All regular expressions can be represented using a finite state machine, and vice versa.
More RegEx Characters • ^ (not in a group) – beginning of the line • $ - the end of the line so • ^ftp – ftp at the beginning of a line • end$ - end at the end of a line • ^$ - a blank line
Groups • [qjk] - Either q or j or k • [^qjk] - Neither q nor j nor k • [a-z] - Anything from a to z inclusive • [^a-z] - No lower case letters • [a-zA-Z] - Any letter • [a-z]+ - Any non-zero sequence of lower case letters
Special characters • \n = A newline • \t = A tab • \w = Any alphanumeric (word) character, The same as [a-zA-Z0-9_] • \W = Any non-word character, the same as [^a-zA-Z0-9_] • \d = Any digit. The same as [0-9] • \D = Any non-digit. The same as [^0-9] • \s = Any whitespace character: space, tab, newline, etc • \S = Any non-whitespace character • \b = A word boundary, outside [ ] only • \B = No word boundary
Challenge 1 • Create a regular expression that will match the name, in lowercase, of each of the days of the week. • Make sure it only matches days of the week – within reason.
Challenge 2 • Create a regular expression for a real number • Eg 1, -1.45, 2002.876, -17
Challenge 3 • Create the smallest regular expression that will match the name (lower case) of every month that has 30 days, and matches no other months. • Do the same for 31 days.
RegEx and FSAs • What is the regular expression associated with this FSA? I think! 0*1+0+1+ But don’t take my word for it
Challenge 4 • Create an FSA for this Regular Expression. 1(1|0)*1*(1|0)+1