Previous

Next


21. Metacharacters - Quantifiers: The Star

  • The star is similar to the question mark, but the star allows the modified character to match zero or more times.
  • So, like the question mark, the star part of a regular expression will always succeed, because zero is allowed.
  • The star gives us a way to get around the problem with the phone numbers we had before. With the following list of numbers:
      1234 5678
      4351-9876
      87547890
      8965  9887
      9885 - 9874
            
    all the spacers in between the groups of 4 numbers were a varying number of spaces and dashes. So, if we used the regex:
      [0-9]*[ -]*[0-9]*
            
    then we'd get what we were looking for! (Notice that we can use a character class just like a single literal character.)
  • Of course, it's not a very specific regular expression, because it also matches other things, like "-", and "123-" and "-123" and "1", and also "" - because everything in the regex is quantified with a star, it will allways succeed! So if there were lines in the file with something other than phone numbers, this regex would be way to general for us to use.
  • As mentioned before, it would depend on what the data we were searching was like as to whether this regex would be suitable or not. (We'll come back to this example again and make it better in a moment.)

Previous

Next

Andrew Hill

For LinuxSA Meeting, 21 November 2000