Python: Regular Expressions
|
|
|
- Brooke Welch
- 9 years ago
- Views:
Transcription
1 Python: Regular Expressions Bruce Beckles Bob Dowling University Computing Service Scientific Computing Support address: 1 Welcome to the University Computing Service s Python: Regular Expressions course. The official UCS address for all scientific computing support queries, including any questions about this course, is: [email protected] 1
2 This course: basic regular expressions getting Python to use them 2 Before we start, let s specify just what is and isn t in this course. This course is a very simple, beginner s course on regular expressions. It mostly covers how to get Python to use them. There is an on-line introduction called the Python Regular Expression HowTo at: and the formal Python documentation at There is a good book on regular expressions in the O Reilly series called Mastering Regular Expressions by Jeffrey E. F. Freidl. Be sure to get the third edition (or later) as its author has added a lot of useful information since the second edition. There are details of this book at: There is also a Wikipedia page on regular expressions which has useful information itself buried within it and a further set of references at the end: 2
3 A regular expression is a pattern describing some text: a series of digits a lower case letter followed by some digits a mixture of characters except for new line, followed by a full stop and one or more letters or numbers \d+ [a-z]\d+.+\.\w+ 3 A regular expression is simply some means to write down a pattern describing some text. (There is a formal mathematical definition but we re not bothering with that here. What the computing world calls regular expressions and what the strict mathematical grammarians call regular expressions are slightly different things.) For example we might like to say a series of digits or a a single lower case letter followed by some digits. There are terms in regular expression language for all of these concepts. 3
4 A regular expression is a pattern describing some text: \d+ Isn't this just gibberish? The language of regular expressions [a-z]\d+.+\.\w+ 4 We will cover what this means in a few slides time. We will start with a trivial regular expression, however, which simply matches a fixed bit of text. 4
5 Classic regular expression filter for each line in a file : Python idiom does the line match a pattern? how can we tell? if it does, output something what? Hey! Something matched! The line that matched The bit of the line that matched 5 This is a course on using regular expressions from Python, so before we introduce even our most trivial expression we should look at how Python drives the regular expression system. Our basic script for this course will run through a file, a line at a time, and compare the line against some regular expression. If the line matches the regular expression the script will output something. That something might be just a notice that it happened (or a line number, or a count of lines matched, etc.) or it might be the line itself. Finally, it might be just the bit of the line that matched. Programs like this, that produce a line of output if a line of input matches some condition and no line of output if it doesn't are called "filters". 5
6 Task: Look for Fred in a list of names Alice Bob Charlotte Derek Ermintrude Fred Freda Frederick Felicity Fred Freda Frederick freds.txt names.txt 6 So we will start with a script that looks for the fixed text Fred in the file names.txt. For each line that matches, the line is printed. For each line that doesn't nothing is printed. 6
7 c.f. grep $ grep 'Fred' < names.txt Fred Freda Frederick $ (Don't panic if you're not a Unix user.) 7 This is equivalent to the traditional Unix command, grep. Don't panic if you're not a Unix user. This is a Python course, not a Unix one. 7
8 Skeleton Python script data flow import sys for input & output import regular expression module define pattern set up regular expression read in the lines one at a time compare line to regular expression for line in sys.stdin: if regular expression matches: sys.stdout.write(line) write out the8 matching lines So we will start with the outline of a Python script and review the non-regular expression lines first. Because we are using standard input and standard output, we will import the sys module to give us sys.stdin and sys.stdout. We will process the file a line at a time. The Python object sys.stdin corresponds to the standard input of the program and if we use it like a list, as we do here, then it behaves like the list of lines in the file. So the Python "for line in sys.stdin:" sets up a for loop running through a line at a time, setting the variable line to be one line of the file after another as the loop repeats. The loop ends when there are no more lines in the file to read. The if statement simply looks at the results of the comparison to see if it was a successful comparison for this particular value of line or not. The sys.stdout.write() line in the script simply prints the line. We could just use print but we will use sys.stdout for symmetry with sys.stdin. The pseudo-script on the slide contains all the non-regular-expression code required. What we have to do now is to fill in the rest: the regular expression components. 8
9 Skeleton Python script reg. exps. import sys import regular expression module module define pattern gibberish prepare the reg. exp. set up regular expression for line in sys.stdin: compare line to regular expression if regular expression matches: sys.stdout.write(line) use the reg. exp. see what we got 9 Now let's look at the regular expression lines we need to complete. Python's regular expression handling is contained in a module so we will have to import that. We will need to write the gibberish that describes the text we are looking for. We need to set up the regular expression in advance of using it. (Actually that's not always true but this pattern is more flexible and more efficient so we'll focus on it in this course.) Finally, for each line we read in we need some way to determine whether our regular expression matches that line or not. 9
10 Loading the module import re regular expressions module 10 The Python module for handling regular expressions is called re. 10
11 Skeleton Python script 1 import sys import re Ready to use regular expressions define pattern set up regular expression for line in sys.stdin: compare line to regular expression if regular expression matches: sys.stdout.write(line) 11 So we add that line to our script. 11
12 Defining the pattern pattern = "Fred" Simple string 12 In this very simple case of looking for an exact string, the pattern is simply that string. So, given that we are looking for "Fred", we set the pattern to be "Fred". 12
13 Skeleton Python script 2 import sys import re pattern = "Fred" Define the pattern set up regular expression for line in sys.stdin: compare line to regular expression if regular expression matches: sys.stdout.write(line) 13 We add this line to our script, but this is just a Python string. We still need to turn it into something that can do the searching for "Fred". 13
14 Setting up a regular expression from the re module compile the pattern Fred regexp = re. compile ( pattern ) regular expression object 14 Next we need to look at how to use a function from this module to set up a regular expression object in Python from that simple string. The re module has a function compile() which takes this string and creates an object Python can do something with. This is deliberately the same word as we use for the processing of source code (text) into machine code (program). Here we are taking a pattern (text) and turning it into the mini-program that does the testing. The result of this compilation is a regular expression object, the mini program that will do work relevant to the particular pattern Fred. We assign the name regexp to this object so we can use it later in the script. 14
15 Skeleton Python script 3 import sys import re pattern = "Fred" regexp = re.compile(pattern) for line in sys.stdin: Prepare the regular expression compare line to regular expression if regular expression matches: sys.stdout.write(line) 15 So we put that compilation line in our script instead of our placeholder. Next we have to apply that regular expression object, regexp, to each line as it is read in to see if the line matches. 15
16 Using a regular expression The reg. exp. object we just prepared. The reg. exp. object's search() method. The text being tested. result = regexp.search(line) The result of the test. 16 We start by doing the test and then we will look at the test's results. The regular expression object that we have just created, regexp, has a method (a built in function specific to itself) called search(). So to reference it in our script we need to refer to regexp.search(). This method takes the text being tested (our input line in this case) as its only argument. The input line in in variable line so we need to run regexp.search(line) to get our result. Note that the string Fred appears nowhere in this line. It is built in to the regexp object. Incidentally, there is a related confusingly similar method called match(). Don't use it. (And that's the only time it will be mentioned in this course.) 16
17 Skeleton Python script 4 import sys import re pattern = "Fred" regexp = re.compile(pattern) for line in sys.stdin: result = regexp.search(line) Use the reg. exp. if regular expression matches : sys.stdout.write(line) 17 So we put that search line in our script instead of our placeholder. Next we have to test the result to see if the search was successful. 17
18 Testing a regular expression's results The result of the regular expression's search() method. Search successful Search unsuccessful tests as True tests as False if result: 18 The search() method returns the Python null object, None, if there is no match and something else (which we will return to later) if there is one. So the result variable now refers to whatever it was that search() returned. None is Python s way of representing nothing. The if test in Python treats None as False and the something else as True so we can use result to provide us with a simple test. 18
19 Skeleton Python script 5 import sys import re pattern = "Fred" regexp = re.compile(pattern) for line in sys.stdin: result = regexp.search(line) See if the if result: line matched sys.stdout.write(line) 19 So if we drop that line into our skeleton Python script we have completed it. This Python script is the fairly generic filter. If a input line matches the pattern write the line out. If it doesn't don't write anything. We will only see two variants of this script in the entire course: in one we only print out certain parts of the line and in the other we allow for there being multiple Freds in a single line. 19
20 Exercise 1(a): complete the file import sys import re pattern = "Fred" regexp = for line in sys.stdin: result = if : sys.stdout.write(line) filter01.py 5 mins 20 If you look in the directory prepared for you, you will find a Python script called filter01.py which contains just this script with a few critical elements missing. Your first exercise is to edit that file to make it a search for the string 'Fred'. Once you have completed the file, test it. 20
21 Exercise 1(b): test your file $ python filter01.py < names.txt Fred Freda Frederick 21 Note that three names match the test pattern: Fred, Freda and Frederick. If you don't get this result go back to the script and correct it. 21
22 Case sensitive matching names.txt Fred Freda Frederick Manfred Python matches are case sensitive by default 22 Note that it did not pick out the name Manfred also in the file. Python regular expressions are case sensitive by default; they do not equate F with f. 22
23 Case insensitive matching regexp = re.compile(pattern,options ) Options are given as module constants: re.ignorecase re.i case insensitive matching and other options (some of which we ll meet later). regexp = re.compile(pattern,re.i ) 23 We can build ourselves a case insensitive regular expression mini-program if we want to. The re.compile() function we saw earlier can take a second, optional argument. This argument is a set of flags which modify how the regular expression works. One of these flags makes it case insensitive. The options are set as a series of values that need to be added together. We re currently only interested in one of them, though, so we can give re.ignorecase (the IGNORECASE constant from the re module) as the second argument. For those of you who dislike long options, the I constant is a synonym for the IGNORECASE constant, so we can use re.i instead of re.ignorecase if we wish. We use re.i in the slide just to make the text fit, but generally we would encourage the long forms as better reminders of what the options mean for when you come back to this script having not looked at it for six months. 23
24 Exercise 2: modify the script 1. Copy filter01.py filter02.py 2. Edit filter02.py Make the search case insensitive. 3. Run filter02.py $ python filter02.py < names.txt 5 mins 24 Copy your answer to the previous exercise into a new file called filter02.py. Edit this new file to make the search case insensitive. This involves a single modification to the compile() line. Then run the new, edited script to see different results. $ cp filter01.py filter02.py $ gedit filter02.py $ python filter02.py < names.txt Fred Freda Frederick Manfred 24
25 Serious example: Post-processing program output RUN COMPLETED. OUTPUT IN FILE hydrogen.dat. RUN COMPLETED. OUTPUT IN FILE helium.dat. RUN COMPLETED. OUTPUT IN FILE yttrium.dat. 1 UNDERFLOW WARNING. RUN COMPLETED. OUTPUT IN FILE zirconium.dat. 2 UNDERFLOW WARNINGS. RUN COMPLETED. OUTPUT IN FILE lanthanum.dat. ALGORITHM DID NOT CONVERGE AFTER ITERATIONS. RUN COMPLETED. OUTPUT IN FILE gadolinium.dat. OVERFLOW ERROR. atoms.log 25 Now let s look at a more serious example. The file atoms.log is the output of a set of programs which do something involving atoms of the elements. (It s a fictitious example, so don t obsess on the detail.) It has a collection of lines corresponding to how various runs of a program completed. Some are simple success lines such as the first line: RUN COMPLETED. OUTPUT IN FILE hydrogen.dat. Others have additional information indicating that things did not go so well. RUN COMPLETED. OUTPUT IN FILE yttrium.dat. 1 UNDERFLOW WARNING. RUN COMPLETED. OUTPUT IN FILE lanthanum.dat. ALGORITHM DID NOT CONVERGE AFTER ITERATIONS. RUN COMPLETED. OUTPUT IN FILE gadolinium.dat. OVERFLOW ERROR. Our job will be to unpick the good lines from the rest. 25
26 What do we want? The file names for the runs with no warning or error messages. RUN COMPLETED. OUTPUT IN FILE sulphur.dat. RUN COMPLETED. OUTPUT IN FILE chlorine.dat. RUN COMPLETED. OUTPUT IN FILE argon.dat. What pattern does this require? 26 We will build the pattern required for these good lines bit by bit. It helps to have some lines in mind while developing the pattern, and to consider which bits change between lines and which bits don't. Because we are going to be using some leading and trailing spaces in our strings we are marking them explicitly in the slides. 26
27 Literal text Fixed text RUN COMPLETED. OUTPUT IN FILE chlorine.dat. 27 The fixed text is shown here. Note that while the element part of the file name varies, its suffix is constant. 27
28 Digits RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Six digits 28 The first part of the line that varies is the set of six digits. Note that we are lucky that it is always six digits. More realistic output might have varying numbers of digits: 2 digits for 17 as in the slide but only one digit for 9. 28
29 Letters RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Sequence of lower case letters 29 The second varying part is the primary part of the file name. 29
30 And no more! RUN COMPLETED. OUTPUT IN FILE chlorine.dat. The line starts here and ends here 30 What we have described to date matches all the lines. They all start with that same sentence. What distinguishes the good lines from the bad is that this is all there is. The lines start and stop with exactly this, no more and no less. It is good practice to match against as much of the line as possible as it lessens the chance of accidentally matching a line you didn t plan to. Later on it will become essential as we will be extracting elements from the line. 30
31 Building the pattern 1 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Start of the line marked with ^ An anchored pattern ^ 31 We will be building the pattern at the bottom of the slide. We start by saying that the line begins here. Nothing may precede it. The start of line is represented with the caret or circumflex character, ^. ^ is known as an anchor, because it forces the pattern to match only at a fixed point (the start, in this case) of the line. Such patterns are called anchored patterns. Patterns which don t have any anchors in them are known as (surprise!) unanchored patterns. 31
32 Building the pattern 2 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Literal text Don't forget the space! ^RUN 32 Next comes some literal text. We just add this to the pattern as is. There s one gotcha we will return to later. It s easy to get the wrong number of spaces or to mistake a tab stop for a space. In this example it s a single space, but we will learn how to cope with generic white space later. 32
33 Building the pattern 3 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Six digits [0-9] any single character between 0 and 9 \d any digit ^RUN \d\d\d\d\d\d inelegant 33 Next comes a run of six digits. There are two approaches we can take here. A digit can be regarded as a character between 0 and 9 in the character set used, but it is more elegant to have a pattern that explicitly says a digit. The sequence [0-9] has the meaning one character between 0 and 9 in the character set. (We will meet this use of square brackets in detail in a few slides time.) The sequence \d means exactly one digit. However, a line of six instances of \d is not particularly elegant. Can you imagine counting them if there were sixty rather than six? 33
34 Building the pattern 4 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Six digits \d any digit \d{6} six digits \d{5,7} five, six or seven digits ^RUN \d{6} 34 Regular expression pattern language has a solution to that inelegance. Following any pattern with a number in curly brackets ( braces ) means to iterate that pattern that many times. Note that \d{6} means six digits in a row. It does not mean the same digit six times. We will see how to describe that later. The syntax can be extended: \d{6} six digits \d{5,7} five, six or seven digits \d{5,} five or more digits \d{,7} no more than seven digits (including no digits) 34
35 Building the pattern 5 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Literal text (with spaces). ^RUN \d{6} COMPLETED. OUTPUT IN FILE 35 Next comes some more fixed text. As ever, don't forget the leading and trailing spaces. 35
36 Building the pattern 6 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Sequence of lower case letters [a-z] any single character between a and z [a-z]+ one or more characters between a and z ^RUN \d{6} COMPLETED. OUTPUT IN FILE [a-z]+ 36 Next comes the name of the element. We will ignore for these purposes the fact that we know these are the names of elements. For our purposes they are sequences of lower case letters. This time we will use the square bracket notation. This is identical to the wild cards used by Unix shells, if you are already familiar with that syntax. The regular expression pattern [aeiou] means exactly one character which can be either an a, an e, an i, an o, or a u. The slight variant [a-m] means exactly one character between a and m inclusive in the character set. In the standard computing character sets (with no internationalisation turned on) the digits, the lower case letters, and the upper case letters form uninterrupted runs. So [0-9] will match a single digit. [a-z] will match a single lower case letter. [A-Z] will match a single upper case letter. But we don t want to match a single lower case letter. We want to match an unknown number of them. Any pattern can be followed by a + to mean repeat the pattern one or more times. So [a-z]+ matches a sequence of one or more lower case letters. (Again, it does not mean the same lower case letter multiple times.) It is equivalent to [a-z]{1,}. 36
37 Building the pattern 7 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Literal text ^RUN \d{6} COMPLETED. OUTPUT IN FILE [a-z]+.dat. 37 Next we have the closing literal text. (Strictly speaking the dot is a special character in regular expressions but we will address that in a later slide.) 37
38 Building the pattern 8 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. End of line marked with $ ^RUN \d{6} COMPLETED. OUTPUT IN FILE [a-z]+.dat.$ 38 Finally, and crucially, we identify this as the end of the line. The lines with warnings and errors go beyond this point. The dollar character, $, marks the end of the line. This is another anchor, since it forces the pattern to match only at another fixed place (the end) of the line. Note that although we are using both ^ and $ in our pattern, you don t have to always use both of them in a pattern. You may use both, or only one, or neither, depending on what you are trying to match. 38
39 Exercise 3(a): running the filter 1. Copy filter01.py filter03.py 2. Edit filter03.py Use the ^RUN regular expression. 3. Test it against atoms.log $ python filter03.py < atoms.log 5 mins 39 You should try this regular expression for yourselves and get your fingers used to typing some of the strange sequences. Copy the filter01.py file that you developed previously to a new file called filter03.py. Then edit the simple Fred string to the new expression we have developed. This search should be case sensitive. Then try it out for real. $ cp filter01.py filter03.py $ gedit filter03.py $ python filter03.py < atoms.log If it doesn't work, go back and fix filter03.py until it does. 39
40 Exercise 3(b): changing the filter 4. Edit filter03.py Lose the $ at the end of the pattern. 5. What output do you think you will get this time? 6. Test it against atoms.log again. $ python filter03.py < atoms.log 7. Put the $ back. 5 mins 40 Then change the regular expression very slightly simply by removing the final dollar character that anchors the expression to the end of the line. What extra lines do you think it will match now. Try the script again. Were you right? 40
41 Special codes in regular expressions \A \Z \d \D ^ $ Anchor start of line Anchor end of line Any digit Any non-digit 41 We have now started to meet some of the special codes that the regular expression language uses in its patterns. The caret character, ^, means start of line. The caret is traditional, but there is an equivalent which is \A. The dollar character, $, means end of line and has a \Z equivalent. The sequence \d means a digit. Note that the capital version, \D means exactly the opposite. 41
42 What can go in [ ]? [aeiou] any lowercase vowel [A-Z] any uppercase alphabetic [A-Za-z] any alphabetic [A-Za-z\]] any alphabetic or a ] backslashed character [A-Za-z\-] [-A-Za-z] any alphabetic or a - any alphabetic or a - - as first character: special behaviour for - only 42 We also need to consider just what can go in between the square brackets. If we have just a set of simple characters (e.g. [aeiou] ) then it matches any one character from that set. Note that the set of simple characters can include a space, e.g. [ aeiou] matches a space or an a or an e or an i or an o or a u. If we put a dash between two characters then it means any one character from that range. So [a-z] is exactly equivalent to [abcdefghijklmnopqrstuvwxyz]. We can repeat this for multiple ranges, so [A-Za-z] is equivalent to [ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]. If we want one of the characters in the set to be a dash, -, there are two ways we can do this. We can precede the dash with a backspace \- to mean include the character - in the set of characters we want to match, e.g. [A-Za-z\-] means match any alphabetic character or a dash. Alternatively, we can make the first character in the set a dash in which case it will be interpreted as a literal dash ( - ) rather than indicating a range of characters, e.g. [-A-za-z] also means match any alphabetic character or a dash. 42
43 What can go in [ ]? [^aeiou] not any lowercase vowel [^A-Z] not any uppercase alphabetic [\^A-Z] any uppercase alphabetic or a caret 43 If the first character in the square brackets is a caret ( ^ ) then the sense of the term is reversed; it stands for any one character that is not one of those in the square brackets. If you want to have a true caret in the set, precede it with a backslash. 43
44 Counting in regular expressions [abc] Any one of a, b or c. [abc]+ One or more a, b or c. [abc]? Zero or one a, b or c. [abc]* Zero or more a, b or c. [abc]{6} Exactly 6 of a, b or c. [abc]{5,7} 5, 6 or 7 of a, b or c. [abc]{5,} 5 or more of a, b or c. [abc]{,7} 7 or fewer of a, b or c. 44 We also saw that we can count in regular expressions. These counting modifiers appear in the slide after the example pattern [abc]. They can follow any regular expression pattern. We saw the plus modifier, +, meaning one or more. There are a couple of related modifiers that are often useful: a query,?, means zero or one of the pattern and asterisk, *, means zero or more. Note that in shell expansion of file names ( globbing ) the asterisk means any string. In regular expressions it means nothing on its own and is purely a modifier. The more precise counting is done wit curly brackets. 44
45 What matches [? [abcd] matches any one of a, b, c or d. What matches [abcd]? [abcd] Any one of a, b, c, d. \[abcd\] [abcd] 45 Now let's pick up a few stray questions that might have arisen as we built that pattern. If square brackets identify sets of letters to match, what matches a square bracket? How would I match the literal string [abcde], for example? The way to mean a real square bracket is to precede it with a backslash. Generally speaking, if a character has a special meaning then preceding it with a backslash turns off that specialness. So [ is special, but \[ means just an open square bracket. (Similarly, if we want to match a backslash we use \\.) We will see more about backslash next. 45
46 Backslash [ ] used to hold sets of characters \[ \] the real square brackets d the letter d \d any digit d \[ \] literal characters \ \d [ ] specials 46 The way to mean a real square bracket is to precede it with a backslash. Generally speaking, if a character has a special meaning then preceding it with a backslash turns off that specialness. So [ is special, but \[ means just an open square bracket. (Similarly, if we want to match a backslash we use \\.) Conversely, if a character is just a plain character then preceding it with a backslash can make it special. For example, d matches just the lower case letter d but \d matches any one digit. 46
47 What does dot match? We ve been using dot as a literal character. Actually.. matches any character except \n. \. \. matches just the dot. 47 There s also an issue with using.. We ve been using it as a literal character, matching the full stops at the ends of sentences, or in file name suffixes but actually it s another special character that matches any single character except for the new line character ( \n matches the new line character). We ve just been lucky so far that the only possible match has been to a real dot. If we want to force the literal character we place a backslash in front of it, just as we did with square brackets. 47
48 Special codes in regular expressions \A \Z ^ $ Anchor start of line Anchor end of line \d \D Any digit Any non-digit. Any character except newline 48 So we can add the full stop to our set of special codes. 48
49 Building the pattern 9 RUN COMPLETED. OUTPUT IN FILE chlorine.dat. Actual full stops in the literal text. ^RUN \d{6} COMPLETED\. OUTPUT IN FILE [a-z]+\.dat\.$ 49 So our filter expression for the atoms.log file needs a small tweak to indicate that the dots are real dots and not just any character except the newline markers. 49
50 Exercise 4: changing the atom filter 1. Edit filter03.py Fix the dots. 2. Run filter03.py again to check it. $ python filter03.py < atoms.log 50 So apply this change to the dots to your script that filters the atoms log. 50
51 Exercise 5 and coffee break Input: messages Script: filter04.py Match lines with Invalid user. Match the whole line. Grow the pattern one bit at a time. 15 mins 51 We ll take a break to grab some coffee. Over the break try this exercise. Copy the file filter03.py to filter04.py and change the pattern to solve this problem: The file messages contains a week s logs from one of the authors workstations. In it are a number of lines containing the phrase Invalid user. Write a regular expression to match these lines and then print them out. Match the whole line, not just the phrase. We will want to use the rest of the line for a later exercise. In addition, it forces you to think about how to match the terms that appear in that line such as dates and time stamps. This is a complex pattern. We strongly suggest building the pattern one bit at a time. Start with ^[A-Z][a-z]{2} to match the month at the start of the line. Get that working. Then add the day of the month. Get that working. Then add the next bit and so on. There are 1,366 matching lines. Obviously, that s too many lines for you to sensibly count just by looking at the screen, so you can use the Unix command wc to do this for you like this: $ python filter04.py < messages wc -l 1366 (The -l option to wc tells it just to count the number of lines in the output. The pipe symbol,, tells the operating system to take the output of your Python script and give it to the wc command as input.) 51
52 Answer to exercise ^[A-Z][a-z]{2} [123 ][0-9] \d\d:\d\d:\d\d noether sshd\[\d+\]: Invalid user [A-Za-z0-9/\-]+ from \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ ^ Start of line [A-Z][a-z]{2} Jan, Feb, Mar, [123 ][0-9] 2, 12, \d\d:\d\d:\d\d 01:23:34, 12:34:50, \[\d+\] [567], [12345] [A-Za-z0-9/\-]+ admin, www-data, mp3, \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} $ End of line , 52 Here s the answer to the exercise. If any of it doesn't make sense, please tell the lecturer now. The patterns aren t perfect. The pattern used to match the three character abbreviation of the month also matches Jeb and Fan, for example. Note how to match dates which run from 1 to 31 we don t say two digits. We say a leading 1, 2, 3 or space followed by an arbitrary digit. Again, this matches some things which are not dates, 32 for example, but does exclude some other not-dates. The pattern [A-Za-z0-9/\-]+ to match the IDs being offered is based purely on visual inspection of those in the file. In the future a hacker may attempt to access the system using a login with a different character in it. We will see how to deal with this uncertainty very soon. 52
53 Tightening up the regular expression \s+ general white space \S+ general non-white space \ Backslash is special in Python strings ( \n ) r' ' Putting an r in front of a string turns off any special treatment of backslash. (Routinely used for regular expressions.) r" " 53 At this point we ought to develop some more syntax and a useful Python trick to help us round some problems which we have skated over. An issue with breaking up lines like this is that some systems use single spaces while others use double spaces at the ends of sentences or tab stops between fields etc. The sequence \s means a white space character (space and tab mostly) so \s+ is commonly used for some white space. Note that white space is marked by a backslashed lower case s. An upper case S means exactly the opposite. \s matches a single white space character and \S matches a single character that is not white space. This will let us work round the problem of not knowing what characters will appear in the invalid logins in advance. We seem to be using backslash a lot in regular expressions. Unfortunately backslash is also special for ordinary Python strings. \n, for example means a new line, \t means a tab, and so on. We want to make sure that our regular expression use of backslash does not clash with the Python use of backslash. The way we do this is to precede the string with the letter r. This turns off any special backslash handling Python would otherwise do. This is usually only done for regular expressions. 53
54 The final pattern ^[A-Z][a-z]{2} [123 ][0-9] \d\d:\d\d:\d\d noether sshd\[\d+\]: Invalid user \S+ from \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ 54 So our exercise s pattern gets this final form. We have replaces the [A-Za-z0-9/\-]+ pattern which happened to work for our particular log file with \S+ (that s an uppercase S ) to match more generally. 54
55 The final string r' ^[A-Z][a-z]{2} [123 ][0-9] \d\d:\d\d:\d\d noether sshd\[\d+\]: Invalid user \S+ from \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ ' 55 And the corresponding Python string in the script gets a leading r to force raw mode to stop any Pythonic interpretation of the backslashes, leaving them for the regular expression system. 55
56 Special codes in regular expressions \A \Z ^ $ Anchor start of line Anchor end of line \d \D Any digit Any non-digit. Any character except newline \s \S Any white-space Any non-white-space \w \W Any word character (letter, digit, "_") 56 Any non-word character So we can add \s and \S to our set of special codes and we will take the opportunity to include just two more. The code \w matches any character that s likely to be in a (computerish) word. These are the letters (both upper and lower case), the digits and the underscore character. There is no punctuation included. The upper case version means the opposite again. 56
57 Exercise 5: improving the filter Copy filter04.py filter05.py [A-Za-z0-9/\-]+ \S+ " " r" " 5 mins 57 Now you can improve your filter04.py script. You have now done all that can really be done to the pattern to make it as good as it gets. 57
58 Example Find the lines containing Jan or Feb or Mar or Apr 58 Our regular expression isn't perfect. Any month abbreviation that starts with an upper case letter followed by two lower case letters will pass. Suppose we really wanted to say Jan or Feb or mar etc. 58
59 Alternation syntax Must be in parentheses A group ( Jan Feb Mar Apr ) Separated by 59 The syntax we use to describe this alternation is as ffollows: We take the expressions the month must match and place them between vertical bars (which we pronounce as or ) and place the whole thing in parentheses (round brackets). Any element placed within round brackets is called a group and we will be meeting groups in their own right later. 59
60 Parentheses in regular expressions Use a group for alternation ( ) Use backslashes for literal parentheses \( Backslash not needed in [ ] \) [a-z()] 60 We will meet parentheses (and groups) a lot in this course so we will start a slide to keep track of their various uses. 60
61 Complex regular expressions ^[A-Z][a-z]{2} [123 ][0-9] \d\d:\d\d:\d\d noether sshd\[\d+\]: Invalid user \S+ from \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ If regular expressions were a programming language comments layout meaningful variable names 61 As we ve just seen, regular expressions can get really complicated. If regular expressions were a programming language in their own right, we would expect to be able to lay them out sensibly to make them easier to read and to include comments. Python allows us to do both of these with a special option to the re.compile() function which we will meet now. (We might also expect to have variables with names, and we will come to that in this course too.) 61
62 Verbose mode ^[A-Z][a-z]{2} [123 ][0-9] \d\d:\d\d:\d\d noether sshd\[\d+\]: Invalid user \S+ from \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ Problems Solutions Hard to write Multi-line layout Harder to read Comments Hardest to maintain 62 Our fundamental problem is that the enormous regular expression we have just written runs the risk of becoming gibberish. It was a struggle to write and if you passed it to someone else it would be even more of a struggle to read. It gets even worse if you are asked to maintain it after not looking at it for six months. The problem is that there is nothing that looks like a useful language for our eyes to hook on; it looks too much like nonsense. We need to be able to spread it out over several lines so that how it breaks down into its component parts becomes clearer. It would be nice if we had comments so we could annotate it too. Python s regular expression system has all this as an option and calls it, rather unfairly, verbose mode. 62
63 Layout What about spaces? Significant space ^ [A-Z][a-z]{2} [123 ][0-9] \d\d:\d\d:\d\d Ignoreable noether sshd \[\d+\]: Invalid user \S+ from \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} $ space Need to treat spaces specially 63 Don't change your script yet! To fix our layout concerns let's try splitting our pattern over several lines. We ll use the one from the exercise as it is by far the most complex pattern we have seen to date. The first issue we hit concerns spaces. We want to match on spaces, and our original regular expression had spaces in it. However, multi-line expressions like this typically have trailing spaces at the ends of lines ignored. In particular any spaces between the end of the line and the start of any putative comments mustn't contribute towards the matching component. We will need to treat spaces differently in the verbose version. 63
64 Layout Backslashes! Significant space ^ [A-Z][a-z]{2}\ [123 ][0-9]\ \d\d:\d\d:\d\d\ Ignoreable noether\ sshd \[\d+\]:\ Invalid\ user\ \S+\ from\ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} $ space 64 We use a backslash. In a verbose regular expression pattern spaces become special characters; special because they are completely ignored. So we make a particular space ordinary (i.e. just a space) by preceding it with a backslash, just as we did for square brackets. Slightly paradoxically, where the space appears inside square brackets to indicate membership of a set of characters it doesn't need backslashing as its meaning is unambiguous. 64
65 Spaces in verbose mode Space ignored generally Backslash space recognised \ Backslash not needed in [ ] [123 ] 65 So this will be a slight change needed to our regular expression language to support multi-line regular expressions. 65
66 Comments ^ [A-Z][a-z]{2}\ # Month [123 ][0-9]\ \d\d:\d\d:\d\d\ noether\ sshd \[\d+\]:\ Invalid\ user\ \S+\ from\ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} $ Significant space Comment Ignored space 66 Now let's add comments. We will introduce them using exactly the same character as is used in Python proper, the hash character, #. Any text from the hash character to the end of the line is ignored. This means that we will have to have some special treatment for hashes if we want to match them as ordinary characters, of course. It's time for another backslash. 66
67 Hashes in verbose mode Hash introduces a comment # Month Backslash hash matches # \# Backslash not needed in [ ] [123#] 67 In multi-line mode, hashes introduce comments. The backslashed hash, \#, matches the hash character itself. Again, just as with space, you don't need the backslash inside square brackets. 67
68 Verbose mode ^ [A-Z][a-z]{2}\ [123 ][0-9]\ \d\d:\d\d:\d\d\ noether\ sshd \[\d+\]:\ Invalid\ user\ \S+\ from\ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} $ # Month # Day # Time # Process ID # User ID # IP address 68 So this gives us our more legible mode. Each element of the regular expression gets a line to itself so it at least looks like smaller pieces of gibberish. Furthermore each can have a comment so we can be reminded of what the fragment is trying to match. 68
69 Python long strings r''' Start raw long string ^ [A-Z][a-z]{2}\ [123 ][0-9]\ \d\d:\d\d:\d\d\ noether\ sshd \[\d+\]:\ Invalid\ user\ \S+\ from\ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} $ ''' End long # Month # Day # Time # Process ID # User ID # IP address string 69 This verbose regular expression pattern covers many lines. Python has a mechanism specifically designed for multi-line strings: the triple-quoted string. We always use that, in conjunction with the r ( raw ) qualifier to carry these verbose regular expression patterns. 69
70 Telling Python to go verbose Verbose mode Another option, like ignoring case Module constant, like re.ignorecase re.verbose re.x 70 So now all we have to do is to tell Python to use this verbose mode instead of its usual one. We do this as an option on the re.compile() function just as we did when we told it to work case insensitively. There is a Python module constant re.verbose which we use in exactly the same way as we did re.ignorecase. It has a chort name re.x too. Incidentally, if you ever wanted case insensitivity and verbosity, you add the two together: regexp = re.compile(pattern, re.ignorecase+re.verbose) 70
71 import sys import re pattern = r"^[a-z][a-z]{2} $ " regexp = re.compile(pattern) import sys import re pattern = r"""^ [A-Z][a-z]{2}\ $""" regexp = re.compile(pattern,re.verbose ) 71 So how would we change a filter script in practice to use verbose regular expressions? It's actually a very easy three step process. 1. Convert your pattern string into a multi-line string. 2. Make the backslash tweaks necessary. 3. Change the re.compile() call to have the re.verbose option. 4. Test your script to see if it still works! 71
72 Exercise 7: use verbose mode filter03.py filter05.py filter04.py filter06.py single line regular expression verbose regular expression 10 mins 72 So now that you ve seen how to turn an ordinary regular expression into a verbose one with comments, it's time to try it for real. Copy the files filter03.py and filter04.py and edit the copies so that the regular expression patterns they use are verbose ones laid out across multiple lines with suitable comments. Test them against the same input files as before. $ cp filter03.py filter05.py $ gedit filter05.py $ python filter05.py < atoms.log $ cp filter04.py filter06.py $ gedit filter06.py $ python filter06.py < messages As ever, if you have any problems with this exercise, please ask the lecturer. In each edit you will need to convert the pattern and set the compilation option. 72
73 Extracting bits from the line ^ # Start of line RUN\ # Job number \d{6} \ COMPLETED\.\ OUTPUT\ IN\ FILE\ [a-z]+\.dat # File name \. $ # End of line Suppose we wanted to extract just these two components. 73 We're almost finished with the regular expression syntax now. We have most of what we need for this course and can now get on with developing Python's system for using it. We will continue to use the verbose version of the regular expressions as it is easier to read, which is helpful for courses as well as for real life! Note that nothing we teach in the remainder of this course is specific to verbose mode; it will all work equally well in the concise mode too. Suppose we are particularly interested in two parts of the line, the job number and the file name. Note that the file name includes both the component that varies from line to line, [a z]+, and the constant, fixed suffix,.dat. What we will do is label the two components in the pattern and then look at Python's mechanism to get at their values. 73
74 Changing the pattern ^ # Start of line RUN\ # Job number (\d{6} ) \ COMPLETED\.\ OUTPUT\ IN\ FILE\ ([a-z]+\.dat) # File name \. $ # End of line Parentheses around the patterns Groups again 74 We start by changing the pattern to place parentheses (round brackets) around the two components of interest. Recall the (Jan Feb Mar Apr) example. These are groups again, but this time they are groups of just one pattern rather than a chain of them. 74
75 The match object regexp = re.compile(pattern,re.verbose) for line in sys.stdin: result = regexp.search(line) if result: 75 Now we are asking for certain parts of the pattern to be specially treated (as groups ) we must turn our attention to the result of the search to get at those groups. To date all we have done with the results is to test them for truth or falsehood: does it match or not? Now we will dig more deeply. 75
76 Using the match object Line: RUN COMPLETED. OUTPUT IN FILE hydrogen.dat. result.group(1) '000001' result.group(2) 'hydrogen.dat' result.group(0) whole pattern 76 We get at the groups from the match object. The method result.group(1) will return the contents of the first pair of parentheses and the method result.group(2) will return the content of the second. Avid Pythonistas will recall that Python usually counts from zero and may wonder what result.group(0) gives. This returns whatever the entire pattern matched. In our case where our regular expression defines the whole line (^ to $) this is equivalent to the whole line. 76
77 Putting it all together regexp = re.compile(pattern,re.verbose) for line in sys.stdin: result = regexp.search(line) if result: sys.stdout.write("%s\t%s\n" % (r esult.group(1), result.group(2))) 77 So now we can write out just those elements of the matching lines that we are interested in. Note that we still have to test the result variable to make sure that it is not None (i.e. that the regular expression matched the line at all). This is what the if test does because None tests false. We cannot ask for the group() method on None because it doesn't have one. If you make this mistake you will get an error message: AttributeError: 'NoneType' object has no attribute 'group' and your script will terminate abruptly. 77
78 Parentheses in regular expressions Use a group for alternation ( ) Use backslashes for literal parentheses \( \) Backslash not needed in [ ] [a-z()] Use a group for selection ( ) 78 If you want to match a literal parenthesis use \( or \). Note that because (unbackslashed) parentheses have this special meaning of defining subsets of the matching line they must match. If they don't then the re.compile() function will give an error similar to this: >>> pattern='(' >>> regexp=re.compile(pattern) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.6/re.py", line 188, in compile return _compile(pattern, flags) File "/usr/lib64/python2.6/re.py", line 243, in _compile raise error, v # invalid expression sre_constants.error: unbalanced parenthesis >>> 78
79 Exercise 8: limited output Modify the log file filter to output just the account name and the IP address. filter06.py filter07.py sys.stdout.write("%s\t%s\n" % (name, address)) 5 mins 79 Now try it for yourselves: You have a file filter06.py which you created to answer an earlier exercise. This finds the lines from the messages file which indicate an Invalid user. Copy this script to filter07.py. Edit filter07.py so that you define groups for the account name (matched by \S+) and the IP address (matched by \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}). $ cp filter06.py filter07.py $ gedit filter07.py $ python filter07.py < messages The bottom of the slide is a quick reminder of the string substitution syntax in Python. This will get you nicely tab-aligned text. 79
80 Limitations of numbered groups The problem: Insert a group All following numbers change What was group number three again? The solution: use names instead of numbers Insert a group It gets its own name Use sensible names. 80 Groups in regular expressions are good but they're not perfect. They suffer from the sort of problem that creeps up on you only after you've been doing Python regular expressions for a bit. Suppose you decide you need to capture another group within a regular expression. If it is inserted between the first and second existing group, say, then the old group number 2 becomes the new number 3, the old 3 the new 4 and so on. There's also a problem that regexp.group(2) doesn't shout out what the second group actually was. There's a solution to this. We will associate names with groups rather than just numbers. 80
81 Named groups A group named jobnum ^ # Start of line RUN\ (?P<jobnum>\d{6}) # Job number \ COMPLETED\.\ OUTPUT\ IN\ FILE\ (?P<filename>[a-z]+\.dat) # File name \. $ # End of line Specifying the name 81 So how do we do this naming? We insert some additional controls immediately after the open parenthesis. In general in Python s regular expression syntax (? introduces something special that may not even be a group (though in this case it is). We specify the name with the rather bizarre syntax?p<groupname>. 81
82 Naming a group ( ) < > (?P< filename>[a-z]+\.dat)?p< > define a named group group name pattern 82 So the group is defined as usual by parentheses (round brackets). Next must come?p to indicate that we are handling a named group. Then comes the name of the group in angle brackets. Finally comes the pattern that actually does the matching. None of the?p< > business is used for matching; it is purely for naming. 82
83 Using the named group Line: RUN COMPLETED. OUTPUT IN FILE hydrogen.dat. result.group('jobnum') '000001' result.group('filename') 'hydrogen.dat' 83 To refer to a group by its name, you simply pass the name to the group() method as a string. You can still also refer to the group by its number. So in the example here, result.group('jobno') is the same as result.group(1), since the first group is named jobno. 83
84 Putting it all together 1 pattern=r''' ^ RUN\ (?P<jobnum>\d{6}) # Job number \ COMPLETED\.\ OUTPUT\ IN\ FILE\ (?P<filename>[a-z]+\.dat) # File name \. $ ''' 84 So if we edit our filter05.py script we can allocate group name jobnum to the series of six digits and filename to the file name (complete with suffix.dat ). This is all done in the pattern string. 84
85 Putting it all together 2 regexp = re.compile(pattern,re.verbose) for line in sys.stdin: result = regexp.search(line) if result: sys.stdout.write("%s\t%s\n" % r esult.group('jobnum'), result.group('fi lename')) 85 At the bottom of the script we then modify the output line to use the names of the groups in the write statement. 85
86 Parentheses in regular expressions Alternation ( ) Backslashes for literal parentheses \( Backslash not needed in [ ] [a-z()] Numbered selection ( ) Named selection (?P<name> ) \) 86 So here s a new use of parentheses: named groups. 86
87 Exercise 9: used named groups filter07.py filter08.py numbered groups named groups 5 mins 87 Now try it for yourselves. Make a copy of the filter07.py script in filter08.py and edit the copy to use named groups (with meaningful group names). Make sure you test it to check it still works! If you have any problems with this exercise, please ask the lecturer. 87
88 Ambiguous groups within the regular expression Dictionary: /var/lib/dict/words Reg. exp.: ^([a-z]+)([a-z]+)$ Script: filter09.py What part of the word goes in group 1, and what part goes in group 2? 88 Groups are all well and good, but are they necessarily well-defined? What happens if a line can fit into groups in two different ways? For example, consider the list of words in /var/lib/dict/words. The lower case words in this line all match the regular expression [a-z]+[a-z]+ because it is a series of lower case letters followed by a series of lower case letters. But if we assign groups to these parts, ([a-z]+)([a-z]+), which part of the word goes into the first group and which in the second? You can find out by running the script filter09.py which is currently in your home directory: $ python filter09.py aa h aahe d aahin g aah s aa l aali i aalii s aal s aardvar k 88
89 Greedy expressions ^ ([a-z]+) ([a-z]+) $ The first group is greedy at the $ python filter09.py expense of the second group. aa h aali i aardvar k Aim to avoid ambiguity aardvark s 89 Python s implementation of regular expressions makes the first group greedy ; the first group swallows as many letters as it can at the expense of the second. There is no guarantee that other languages implementations will do the same, though. You should always aim to avoid this sort of ambiguity. You can change the greed of various groups with yet more use of the query character but please note the ambiguity caution above. If you find yourself wanting to play with the greediness you're almost certainly doing something wrong at a deeper level. 89
90 Referring to numbered groups within a regular expression Matches the same as group 1 ^ ([a-z]+) \1 $ Matches a sequence of letters e.g. beriberi tsetse Group 1 90 In our ambiguity example, filter09.py, we had the same pattern, [a-z]+, repeated twice. These then matched against different strings. The first matched against aardvar and the second against k, for example. How can we say that we want the same string twice? Now that we have groups in our regular expression we can use them for this purpose. So far the bracketing to create groups has been purely labelling, to select sections we can extract later. Now we will use them within the expression itself. We can use a backslash in front of a number (for integers from 1 to 99) to mean that number group in the current expression. The pattern ^([a-z]+)\1$ matches any string which is followed by the string itself again. 90
91 Referring to named groups within a regular expression ^ (?P<half>[a-z]+) Creates a group, half (?P=half) Refers to the group $ Does not create a group 91 If we have given names to our groups, then we use the special Python syntax (?P=groupname) to mean the group groupname in the current expression. So ^(?P<word>[a-z]+)(?P=word)$ matches any string which is the same sequence of lower case letters repeated twice. Note that in this case the (? ) expression does not create a group; instead, it refers to one that already exists. Observe that there is no pattern language in that second pair of parentheses. 91
92 Example $ python filter10.py atlatl baba beriberi bonbon booboo bulbul 92 The file filter06.py does precisely this using a named group. I have no idea what half of these words mean. 92
93 Parentheses in regular expressions Alternation ( ) Backslashes for literal parentheses \( Backslash not needed in [ ] [a-z()] Numbered selection ( ) Named selection (?P<name> ) Named reference (?P=name) \) 93 This completes the next set of uses of parentheses in Python regular expressions. Remember that the final reference example does not create a group. 93
94 Exercise 10 filter10.py filter11.py Find all the words with the pattern ABABA e.g. entente entente A A A e B B nt 94 Copy the script filter10.py to filter11.py and edit the latter to find all the words with the form ABABA. (Call your groups a and b if you are stuck for meaningful names. Note that in for the example word on the slide, the A pattern just happens to be one letter long (the lower case letter e ), whilst the B pattern is two letters long (the lower case letter sequence nt ). Hint: On PWF Linux the /var/lib/dict/words dictionary contains 5 such words. No, I have no idea what most of them mean, either. 94
95 Multiple matches Data: boil.txt Basic entry: Ar 87.3 Different number of entries on each line: Ar 87.3 Re Ra K Rn Rh Want to unpick this mess 95 Now we will move on to a more powerful use of groups. Consider the file boil.txt. This contains the boiling points (in Kelvin at standard pressure) of the various elements but it has lines with different numbers of entries on them. Some lines have a single element/temperature pair, others have two, three, or four. We will presume that we don't know what the maximum per line is. 95
96 What pattern do we need? [A-Z][a-z]? element \s+ white space \d+\.\d+ boil K Rn Rh We need it multiple times but we don't know how many96 The basic structure of each line is straightforward, so long as we can have an arbitrary number of instances of a group. 96
97 Elementary pattern (?P<element>[A-Z][a-z]?) \s+ (?P<boil>\d+\.\d+) K Matches a single pair 97 We start by building the basic pattern that will be repeated multiple times. The basic pattern contains two groups which isolate the components we want from each repeat: the name of the element and the temperature. Note that because the pattern can occur anywhere in the line we don't use the ^/$ anchors. 97
98 Putting it all together 1 pattern=r''' (?P<element>[A-Z][a-z]?) \s+ (?P<boil>\d+\.\d+) ''' regexp = re.compile(pattern,re.verbose) 98 We put all this together in a file call filter12.py. 98
99 Putting it all together 2 for line in sys.stdin: result = regexp.search(line) if result: sys.stdout.write("%s\t%s\n" % r esult.group('element'), result.group('b oil')) 99 At the bottom of the script we print out whatever the two groups have matched. 99
100 Try that pattern $ python filter12.py < boil.txt Ar Re K Ag Au First matching case of each line But only the first 100 We will start by dropping this pattern into our standard script, mostly to see what happens. The script does generate some output, but the pattern only matches against the start of the line. It finishes as soon as it has matched once. 100
101 Multiple matches regexp.search(line) returns a single match regexp.finditer (line) returns a list of matches It would be better called searchiter() but never mind 101 The problem lies in our use of regexp's search() method. It returns a single MatchObject, corresponding to that first instance of the pattern in the line. The regular expression object has another method called finditer() which returns a list of matches, one for each that it finds in the line. (It would be better called searchiter() but never mind.) (Actually, it doesn't return a list, but rather one of those Python objects that can be treated like a list. They're called iterators which is where the name of the method comes from.) 101
102 The original script for line in sys.stdin: result = regexp.search(line) if result: sys.stdout.write("%s\t%s\n" % r esult.group('element'), result.group('b oil')) 102 So, we return to our script and observe that it currently uses search()to return a single MatchObject and tests on that object. 102
103 The changed script for line in sys.stdin: results = regexp.finditer(line) for result in results: sys.stdout.write("%s\t%s\n" % r esult.group('element'), result.group('b oil')) 103 The pattern remains exactly the same. We change the line that called search() and stored a single MatchObject for a line that calls finditer() and stores a list of MatchObjects. Instead of the if statement we have a for statement to loop through all of the MatchObjects in the list. (If none are found it s an empty list.) This script can be found in filter13.py. 103
104 Using finditer() $ python filter13.py < boil.txt Ar Re Ra Au At In Every matching case in each line 104 And it works! This time we get all the element/temperature pairs. 104
105 Exercise 11 filter14.py Edit the script so that text is split into one word per line with no punctuation or spaces output. $ python filter14.py < paragraph.txt This is free 105 One last exercise in class. The file filter14.py that you have is a skeleton script that needs lines completed. Edit the file so that it can be used to split incoming text into individual words, printing one on each line. Punctuation should not be printed. You may find it useful to recall the definition of \w. 105
106 We have covered only simple regular expressions! Capable of much more! We have focused on getting Python to use them. UCS course: Pattern Matching Using Regular Expressions focuses on the expressions themselves and not on the language using them. 106 And that's it! However, let me remind you that this course has concentrated on getting regular expressions to work in Python and has only introduced regular expression syntax where necessary to illustrate features in Python's re module. Regular expressions are capable of much, much more and the UCS offers a two afternoon course, Pattern Matching Using Regular Expressions, that covers them in full detail. For further details of this course see the course description at: 106
Python Lists and Loops
WEEK THREE Python Lists and Loops You ve made it to Week 3, well done! Most programs need to keep track of a list (or collection) of things (e.g. names) at one time or another, and this week we ll show
Hands-On UNIX Exercise:
Hands-On UNIX Exercise: This exercise takes you around some of the features of the shell. Even if you don't need to use them all straight away, it's very useful to be aware of them and to know how to deal
Introduction to Python
WEEK ONE Introduction to Python Python is such a simple language to learn that we can throw away the manual and start with an example. Traditionally, the first program to write in any programming language
Regular Expressions. Abstract
Regular Expressions Sanjiv K. Bhatia Department of Mathematics & Computer Science University of Missouri St. Louis St. Louis, MO 63121 email: [email protected] Abstract Regular expressions provide a powerful
Python Loops and String Manipulation
WEEK TWO Python Loops and String Manipulation Last week, we showed you some basic Python programming and gave you some intriguing problems to solve. But it is hard to do anything really exciting until
CS 1133, LAB 2: FUNCTIONS AND TESTING http://www.cs.cornell.edu/courses/cs1133/2015fa/labs/lab02.pdf
CS 1133, LAB 2: FUNCTIONS AND TESTING http://www.cs.cornell.edu/courses/cs1133/2015fa/labs/lab02.pdf First Name: Last Name: NetID: The purpose of this lab is to help you to better understand functions:
Lecture 18 Regular Expressions
Lecture 18 Regular Expressions Many of today s web applications require matching patterns in a text document to look for specific information. A good example is parsing a html file to extract tags
Regular Expressions. General Concepts About Regular Expressions
Regular Expressions This appendix explains regular expressions and how to use them in Cisco IOS software commands. It also provides details for composing regular expressions. This appendix has the following
Programming Languages CIS 443
Course Objectives Programming Languages CIS 443 0.1 Lexical analysis Syntax Semantics Functional programming Variable lifetime and scoping Parameter passing Object-oriented programming Continuations Exception
Programming in Access VBA
PART I Programming in Access VBA In this part, you will learn all about how Visual Basic for Applications (VBA) works for Access 2010. A number of new VBA features have been incorporated into the 2010
VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0
VISUAL GUIDE to RX Scripting for Roulette Xtreme - System Designer 2.0 UX Software - 2009 TABLE OF CONTENTS INTRODUCTION... ii What is this book about?... iii How to use this book... iii Time to start...
University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python
Introduction Welcome to our Python sessions. University of Hull Department of Computer Science Wrestling with Python Week 01 Playing with Python Vsn. 1.0 Rob Miles 2013 Please follow the instructions carefully.
We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.
LING115 Lecture Note Session #4 Python (1) 1. Introduction As we have seen in previous sessions, we can use Linux shell commands to do simple text processing. We now know, for example, how to count words.
Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, 2008. 1 Introduction 1. 2 Invoking Shell Scripts 2
Unix Shell Scripts Norman Matloff July 30, 2008 Contents 1 Introduction 1 2 Invoking Shell Scripts 2 2.1 Direct Interpretation....................................... 2 2.2 Indirect Interpretation......................................
Using Regular Expressions in Oracle
Using Regular Expressions in Oracle Everyday most of us deal with multiple string functions in Sql. May it be for truncating a string, searching for a substring or locating the presence of special characters.
JavaScript: Introduction to Scripting. 2008 Pearson Education, Inc. All rights reserved.
1 6 JavaScript: Introduction to Scripting 2 Comment is free, but facts are sacred. C. P. Scott The creditor hath a better memory than the debtor. James Howell When faced with a decision, I always ask,
Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.
Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to
Regular Expression Searching
Regular Expression Searching Regular expressions allow forensics analysts to search through large quantities of text information for patterns of data such as the following: Telephone Numbers Social Security
Chapter 2. Making Shapes
Chapter 2. Making Shapes Let's play turtle! You can use your Pencil Turtle, you can use yourself, or you can use some of your friends. In fact, why not try all three? Rabbit Trail 4. Body Geometry Can
Databases in Microsoft Access David M. Marcovitz, Ph.D.
Databases in Microsoft Access David M. Marcovitz, Ph.D. Introduction Schools have been using integrated programs, such as Microsoft Works and Claris/AppleWorks, for many years to fulfill word processing,
Mathematical Induction
Mathematical Induction In logic, we often want to prove that every member of an infinite set has some feature. E.g., we would like to show: N 1 : is a number 1 : has the feature Φ ( x)(n 1 x! 1 x) How
Exercise 4 Learning Python language fundamentals
Exercise 4 Learning Python language fundamentals Work with numbers Python can be used as a powerful calculator. Practicing math calculations in Python will help you not only perform these tasks, but also
Independent samples t-test. Dr. Tom Pierce Radford University
Independent samples t-test Dr. Tom Pierce Radford University The logic behind drawing causal conclusions from experiments The sampling distribution of the difference between means The standard error of
Pre-Algebra Lecture 6
Pre-Algebra Lecture 6 Today we will discuss Decimals and Percentages. Outline: 1. Decimals 2. Ordering Decimals 3. Rounding Decimals 4. Adding and subtracting Decimals 5. Multiplying and Dividing Decimals
Excel: Introduction to Formulas
Excel: Introduction to Formulas Table of Contents Formulas Arithmetic & Comparison Operators... 2 Text Concatenation... 2 Operator Precedence... 2 UPPER, LOWER, PROPER and TRIM... 3 & (Ampersand)... 4
AN INTRODUCTION TO UNIX
AN INTRODUCTION TO UNIX Paul Johnson School of Mathematics September 24, 2010 OUTLINE 1 SHELL SCRIPTS Shells 2 COMMAND LINE Command Line Input/Output 3 JOBS Processes Job Control 4 NETWORKING Working From
Encoding Text with a Small Alphabet
Chapter 2 Encoding Text with a Small Alphabet Given the nature of the Internet, we can break the process of understanding how information is transmitted into two components. First, we have to figure out
Kiwi Log Viewer. A Freeware Log Viewer for Windows. by SolarWinds, Inc.
Kiwi Log Viewer A Freeware Log Viewer for Windows by SolarWinds, Inc. Kiwi Log Viewer displays text based log files in a tabular format. Only a small section of the file is read from disk at a time which
Lecture 2 Mathcad Basics
Operators Lecture 2 Mathcad Basics + Addition, - Subtraction, * Multiplication, / Division, ^ Power ( ) Specify evaluation order Order of Operations ( ) ^ highest level, first priority * / next priority
1. The RSA algorithm In this chapter, we ll learn how the RSA algorithm works.
MATH 13150: Freshman Seminar Unit 18 1. The RSA algorithm In this chapter, we ll learn how the RSA algorithm works. 1.1. Bob and Alice. Suppose that Alice wants to send a message to Bob over the internet
Prepare your result file for input into SPSS
Prepare your result file for input into SPSS Isabelle Darcy When you use DMDX for your experiment, you get an.azk file, which is a simple text file that collects all the reaction times and accuracy of
CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.
CHAPTER 2 Logic 1. Logic Definitions 1.1. Propositions. Definition 1.1.1. A proposition is a declarative sentence that is either true (denoted either T or 1) or false (denoted either F or 0). Notation:
Computer Programming In QBasic
Computer Programming In QBasic Name: Class ID. Computer# Introduction You've probably used computers to play games, and to write reports for school. It's a lot more fun to create your own games to play
Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl
First CGI Script and Perl Perl in a nutshell Prof. Rasley shebang line tells the operating system where the Perl interpreter is located necessary on UNIX comment line ignored by the Perl interpreter End
Regular Expressions. In This Appendix
A Expressions In This Appendix Characters................... 888 Delimiters................... 888 Simple Strings................ 888 Special Characters............ 888 Rules....................... 891
OA3-10 Patterns in Addition Tables
OA3-10 Patterns in Addition Tables Pages 60 63 Standards: 3.OA.D.9 Goals: Students will identify and describe various patterns in addition tables. Prior Knowledge Required: Can add two numbers within 20
3. Mathematical Induction
3. MATHEMATICAL INDUCTION 83 3. Mathematical Induction 3.1. First Principle of Mathematical Induction. Let P (n) be a predicate with domain of discourse (over) the natural numbers N = {0, 1,,...}. If (1)
Euler s Method and Functions
Chapter 3 Euler s Method and Functions The simplest method for approximately solving a differential equation is Euler s method. One starts with a particular initial value problem of the form dx dt = f(t,
Thirty Useful Unix Commands
Leaflet U5 Thirty Useful Unix Commands Last revised April 1997 This leaflet contains basic information on thirty of the most frequently used Unix Commands. It is intended for Unix beginners who need a
A PRACTICAL GUIDE TO db CALCULATIONS
A PRACTICAL GUIDE TO db CALCULATIONS This is a practical guide to doing db (decibel) calculations, covering most common audio situations. You see db numbers all the time in audio. You may understand that
Programming Exercises
s CMPS 5P (Professor Theresa Migler-VonDollen ): Assignment #8 Problem 6 Problem 1 Programming Exercises Modify the recursive Fibonacci program given in the chapter so that it prints tracing information.
Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.
1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays
University Convocation. IT 3203 Introduction to Web Development. Pattern Matching. Why Match Patterns? The Search Method. The Replace Method
IT 3203 Introduction to Web Development Regular Expressions October 12 Notice: This session is being recorded. Copyright 2007 by Bob Brown University Convocation Tuesday, October 13, 11:00 AM 12:15 PM
Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration
Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration This JavaScript lab (the last of the series) focuses on indexing, arrays, and iteration, but it also provides another context for practicing with
VHDL Test Bench Tutorial
University of Pennsylvania Department of Electrical and Systems Engineering ESE171 - Digital Design Laboratory VHDL Test Bench Tutorial Purpose The goal of this tutorial is to demonstrate how to automate
Formal Languages and Automata Theory - Regular Expressions and Finite Automata -
Formal Languages and Automata Theory - Regular Expressions and Finite Automata - Samarjit Chakraborty Computer Engineering and Networks Laboratory Swiss Federal Institute of Technology (ETH) Zürich March
CS106A, Stanford Handout #38. Strings and Chars
CS106A, Stanford Handout #38 Fall, 2004-05 Nick Parlante Strings and Chars The char type (pronounced "car") represents a single character. A char literal value can be written in the code using single quotes
PL / SQL Basics. Chapter 3
PL / SQL Basics Chapter 3 PL / SQL Basics PL / SQL block Lexical units Variable declarations PL / SQL types Expressions and operators PL / SQL control structures PL / SQL style guide 2 PL / SQL Block Basic
USC Marshall School of Business Marshall Information Services
USC Marshall School of Business Marshall Information Services Excel Dashboards and Reports The goal of this workshop is to create a dynamic "dashboard" or "Report". A partial image of what we will be creating
How to Write a Simple Makefile
Chapter 1 CHAPTER 1 How to Write a Simple Makefile The mechanics of programming usually follow a fairly simple routine of editing source files, compiling the source into an executable form, and debugging
Excel macros made easy
IT Training Excel macros made easy Jane Barrett, IT Training & Engagement Team Information System Services Version 1.1 Scope Learning outcomes Understand the concept of what a macro is and what it does.
Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.
Unit 1 Number Sense In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions. BLM Three Types of Percent Problems (p L-34) is a summary BLM for the material
If A is divided by B the result is 2/3. If B is divided by C the result is 4/7. What is the result if A is divided by C?
Problem 3 If A is divided by B the result is 2/3. If B is divided by C the result is 4/7. What is the result if A is divided by C? Suggested Questions to ask students about Problem 3 The key to this question
So you want to create an Email a Friend action
So you want to create an Email a Friend action This help file will take you through all the steps on how to create a simple and effective email a friend action. It doesn t cover the advanced features;
Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.
Retrieving Data Using the SQL SELECT Statement Objectives After completing this lesson, you should be able to do the following: List the capabilities of SQL SELECT statements Execute a basic SELECT statement
Cryptography and Network Security Department of Computer Science and Engineering Indian Institute of Technology Kharagpur
Cryptography and Network Security Department of Computer Science and Engineering Indian Institute of Technology Kharagpur Module No. # 01 Lecture No. # 05 Classic Cryptosystems (Refer Slide Time: 00:42)
Eventia Log Parsing Editor 1.0 Administration Guide
Eventia Log Parsing Editor 1.0 Administration Guide Revised: November 28, 2007 In This Document Overview page 2 Installation and Supported Platforms page 4 Menus and Main Window page 5 Creating Parsing
Computational Mathematics with Python
Computational Mathematics with Python Basics Claus Führer, Jan Erik Solem, Olivier Verdier Spring 2010 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 1
Basic numerical skills: FRACTIONS, DECIMALS, PROPORTIONS, RATIOS AND PERCENTAGES
Basic numerical skills: FRACTIONS, DECIMALS, PROPORTIONS, RATIOS AND PERCENTAGES. Introduction (simple) This helpsheet is concerned with the ways that we express quantities that are not whole numbers,
Moving from CS 61A Scheme to CS 61B Java
Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you
Decision Making under Uncertainty
6.825 Techniques in Artificial Intelligence Decision Making under Uncertainty How to make one decision in the face of uncertainty Lecture 19 1 In the next two lectures, we ll look at the question of how
Access Queries (Office 2003)
Access Queries (Office 2003) Technical Support Services Office of Information Technology, West Virginia University OIT Help Desk 293-4444 x 1 oit.wvu.edu/support/training/classmat/db/ Instructor: Kathy
1.6 The Order of Operations
1.6 The Order of Operations Contents: Operations Grouping Symbols The Order of Operations Exponents and Negative Numbers Negative Square Roots Square Root of a Negative Number Order of Operations and Negative
Lecture 4. Regular Expressions grep and sed intro
Lecture 4 Regular Expressions grep and sed intro Previously Basic UNIX Commands Files: rm, cp, mv, ls, ln Processes: ps, kill Unix Filters cat, head, tail, tee, wc cut, paste find sort, uniq comm, diff,
Command Line - Part 1
Command Line - Part 1 STAT 133 Gaston Sanchez Department of Statistics, UC Berkeley gastonsanchez.com github.com/gastonstat Course web: gastonsanchez.com/teaching/stat133 GUIs 2 Graphical User Interfaces
Udacity cs101: Building a Search Engine. Extracting a Link
Udacity cs101: Building a Search Engine Unit 1: How to get started: your first program Extracting a Link Introducing the Web Crawler (Video: Web Crawler)... 2 Quiz (Video: First Quiz)...2 Programming (Video:
Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products
Chapter 3 Cartesian Products and Relations The material in this chapter is the first real encounter with abstraction. Relations are very general thing they are a special type of subset. After introducing
Computational Mathematics with Python
Boolean Arrays Classes Computational Mathematics with Python Basics Olivier Verdier and Claus Führer 2009-03-24 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 1 / 40
Summation Algebra. x i
2 Summation Algebra In the next 3 chapters, we deal with the very basic results in summation algebra, descriptive statistics, and matrix algebra that are prerequisites for the study of SEM theory. You
Excel 2003 A Beginners Guide
Excel 2003 A Beginners Guide Beginner Introduction The aim of this document is to introduce some basic techniques for using Excel to enter data, perform calculations and produce simple charts based on
Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment
.i.-' `-. i..' `/ \' _`.,-../ o o \.' ` ( / \ ) \\\ (_.'.'"`.`._) /// \\`._(..: :..)_.'// \`. \.:-:. /.'/ `-i-->..
1. The Fly In The Ointment
Arithmetic Revisited Lesson 5: Decimal Fractions or Place Value Extended Part 5: Dividing Decimal Fractions, Part 2. The Fly In The Ointment The meaning of, say, ƒ 2 doesn't depend on whether we represent
Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters
Secrets of Professor Don Colton Brigham Young University Hawaii is the C language function to do formatted printing. The same function is also available in PERL. This paper explains how works, and how
CS2043 - Unix Tools & Scripting Lecture 9 Shell Scripting
CS2043 - Unix Tools & Scripting Lecture 9 Shell Scripting Spring 2015 1 February 9, 2015 1 based on slides by Hussam Abu-Libdeh, Bruno Abrahao and David Slater over the years Announcements Coursework adjustments
Session 7 Fractions and Decimals
Key Terms in This Session Session 7 Fractions and Decimals Previously Introduced prime number rational numbers New in This Session period repeating decimal terminating decimal Introduction In this session,
CHAPTER 5 Round-off errors
CHAPTER 5 Round-off errors In the two previous chapters we have seen how numbers can be represented in the binary numeral system and how this is the basis for representing numbers in computers. Since any
Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2
CS 70 Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2 Proofs Intuitively, the concept of proof should already be familiar We all like to assert things, and few of us
Unix the Bare Minimum
Unix the Bare Minimum Norman Matloff September 27, 2005 c 2001-2005, N.S. Matloff Contents 1 Purpose 2 2 Shells 2 3 Files and Directories 4 3.1 Creating Directories.......................................
Microsoft Access 2010 Overview of Basics
Opening Screen Access 2010 launches with a window allowing you to: create a new database from a template; create a new template from scratch; or open an existing database. Open existing Templates Create
Regular Expressions and Automata using Haskell
Regular Expressions and Automata using Haskell Simon Thompson Computing Laboratory University of Kent at Canterbury January 2000 Contents 1 Introduction 2 2 Regular Expressions 2 3 Matching regular expressions
6.170 Tutorial 3 - Ruby Basics
6.170 Tutorial 3 - Ruby Basics Prerequisites 1. Have Ruby installed on your computer a. If you use Mac/Linux, Ruby should already be preinstalled on your machine. b. If you have a Windows Machine, you
Create a survey using Google Forms
Create a survey using Google Forms You can plan events, make a survey or poll, give students a quiz, or collect other information in an easy, streamlined way with Google Forms. Google Forms can be connected
Access Control and Audit Trail Software
Varian, Inc. 2700 Mitchell Drive Walnut Creek, CA 94598-1675/USA Access Control and Audit Trail Software Operation Manual Varian, Inc. 2002 03-914941-00:3 Table of Contents Introduction... 1 Access Control
BreezingForms Guide. 18 Forms: BreezingForms
BreezingForms 8/3/2009 1 BreezingForms Guide GOOGLE TRANSLATE FROM: http://openbook.galileocomputing.de/joomla15/jooml a_18_formulare_neu_001.htm#t2t32 18.1 BreezingForms 18.1.1 Installation and configuration
Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving
Section 7 Algebraic Manipulations and Solving Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving Before launching into the mathematics, let s take a moment to talk about the words
Math 4310 Handout - Quotient Vector Spaces
Math 4310 Handout - Quotient Vector Spaces Dan Collins The textbook defines a subspace of a vector space in Chapter 4, but it avoids ever discussing the notion of a quotient space. This is understandable
MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.
MATH10212 Linear Algebra Textbook: D. Poole, Linear Algebra: A Modern Introduction. Thompson, 2006. ISBN 0-534-40596-7. Systems of Linear Equations Definition. An n-dimensional vector is a row or a column
Excel 2007 A Beginners Guide
Excel 2007 A Beginners Guide Beginner Introduction The aim of this document is to introduce some basic techniques for using Excel to enter data, perform calculations and produce simple charts based on
Lesson 26: Reflection & Mirror Diagrams
Lesson 26: Reflection & Mirror Diagrams The Law of Reflection There is nothing really mysterious about reflection, but some people try to make it more difficult than it really is. All EMR will reflect
Computer Science for San Francisco Youth
Python for Beginners Python for Beginners Lesson 0. A Short Intro Lesson 1. My First Python Program Lesson 2. Input from user Lesson 3. Variables Lesson 4. If Statements How If Statements Work Structure
Operations and Supply Chain Management Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology Madras
Operations and Supply Chain Management Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology Madras Lecture - 41 Value of Information In this lecture, we look at the Value
COMSC 100 Programming Exercises, For SP15
COMSC 100 Programming Exercises, For SP15 Programming is fun! Click HERE to see why. This set of exercises is designed for you to try out programming for yourself. Who knows maybe you ll be inspired to
X1 Professional Client
X1 Professional Client What Will X1 Do For Me? X1 instantly locates any word in any email message, attachment, file or Outlook contact on your PC. Most search applications require you to type a search,
Pigeonhole Principle Solutions
Pigeonhole Principle Solutions 1. Show that if we take n + 1 numbers from the set {1, 2,..., 2n}, then some pair of numbers will have no factors in common. Solution: Note that consecutive numbers (such
Mascot Search Results FAQ
Mascot Search Results FAQ 1 We had a presentation with this same title at our 2005 user meeting. So much has changed in the last 6 years that it seemed like a good idea to re-visit the topic. Just about
grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print
grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print In the simplest terms, grep (global regular expression print) will search input
6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, 2010. Class 4 Nancy Lynch
6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, 2010 Class 4 Nancy Lynch Today Two more models of computation: Nondeterministic Finite Automata (NFAs)
3.GETTING STARTED WITH ORACLE8i
Oracle For Beginners Page : 1 3.GETTING STARTED WITH ORACLE8i Creating a table Datatypes Displaying table definition using DESCRIBE Inserting rows into a table Selecting rows from a table Editing SQL buffer
How to Make the Most of Excel Spreadsheets
How to Make the Most of Excel Spreadsheets Analyzing data is often easier when it s in an Excel spreadsheet rather than a PDF for example, you can filter to view just a particular grade, sort to view which
