| <- Prev Lesson | Next Lesson -> |
Regular expressions (which you will learn a great deal more about in courses like CSE 262) are a way of representing a string or group of strings that share common characteristics. This is done by creating a 'pattern' to which strings can be compared or 'matched.' For instance, the pattern 'hello' matches the strings 'hello', 'It's nice to say hello', and 'I say hello, you say goodbye.', among others.
Generally, we want a more versatile tool at our disposal, and matching simple strings is not very powerful. Therefore, we have other regular expression constructs to aid us in our string matching. Note that different regular expression engines might use different symbols for these constructs. What follows are six of the more commonly used symbols.
| Reg. Exp. | Matches Strings... | Examples |
|---|---|---|
| ^(hello)+$ | that contain only the word hello 1 or more times | "hello", "hellohello", "hellohellohello", etc. |
| ^(hello)*$ | that contain only the word hello 0 or more times | "", "hello", "hellohello", etc. |
| hello|goodbye | that contain either 'hello' or 'goodbye' | "goodbye", "hello", "hello, goodbye", "I like to say hello", "I hate to say goodbye", etc. |
| (good|bad)? | that may or may not contain 'good' or 'bad' | "hello", "", "goodbye", "bad dog!", "goodfellows", etc. |
| <- Prev Lesson | Next Lesson -> |
Designed by D. Kaminsky
© University of Pennsylvania, 2003