Week -5 - Regular Expressions

  1. Read http://www.uccs.edu/~ahitchco/grep/
  2. Log on to igor (or any other Unix system) and type:
    grep -E "^a...d$" /usr/share/dict/words
    

    Describe the output.

  3. type
    cat  /usr/share/dict/words
    

  4. type
    cat  /usr/share/dict/words |wc -l
    

  5. How many words are there that start with a and end with k? What are they?

  6. use grep to find the following:
    1. All the words that start with 'A' or 'a' and end with a 'c' and have at least one 'b' somewhere in between.
    2. All the words that start with 'A' or ('a' followed by zero or more 'b's and end with a 'c' and have at least one 'b' somewhere in between.
    3. Find all the words with all five vowels a,e,i,o,u occurring in order.
    4. Find all the words that do not have any vowels.
    5. Find all the filenames on the system that contain your username. Use the 'locate' comand.
    6. Find all the words that end with 'and'.
    7. Find all the words that contain an 'a' and a 'b'.
    8. Find all the words that contain exactly one 'a' and exactly one 'b'.

  7. Write a Javascript function which checks whether a list is a palindrome.
  8. Think about how regular expressions help to solve the `crossword' assignment.
  9. Read http://eloquentjavascript.net/chapter10.html and look at http://www.javascriptkit.com/javatutors/re.shtml.

  10. The following code allows the user to input numbers with or without a decimal point and then adds one to the value if it is a legal number. Although, accptable, this program allows the user to enter values with leading zeroes like 00123.5. We want to disallow such input. However, we do allow a single leading zero if it comes immediately before the decimal point as in 0.54 and also if the number is 0 itself. Cange the code to correctly achieve this requirement correctly achieve this requirement:

    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <p>
    Result: <output type=text id="resultInt1" value="" readonly> </output>
    </p>
    <input onchange="
    var k=/^[0-9]+(|\.[0-9]+)$/;
    if (k.test(this.value))
    document.getElementById('resultInt1').value=parseFloat(this.value)+1;
    else {alert(this.value + ' is not a number'); this.value='';}
    "
    >
    </BODY>
    </HTML>
    
s.danicic@gold.ac.uk
Sebastian Danicic BSc MSc PhD (Reader in Computer Science)
Dept of Computing, Goldsmiths, University of London, London SE14 6NW
Last updated 2012-07-03