Docsity

Regular Expression Examples and Questions - Prof. Stephany S. Coffman-Wolph, Assignments of Computer Science

Toggle sidebar
bg1
Regular Expression Example Questions:
Write Regular expressions to do the following:
1. Match 0 or more characters:
.*
2. Match 1 or more characters:
.+
3. Match any sequence of letters in upper or lower case, but there
must be at least 1 letter:
[a-zA-Z]+
4. Match any letter that is the range of a to d (lowercase):
[a-d]
5. Match any character that is not in the range a to d:
[^a-d]
6. Match any sequence of at least one word character:
\w+
7. Match a sequence of exactly four word characters:
\w{4} or \w\w\w\w or [a-zA-Z_]{4}
8. Match any digit:
\d or [0-9]
9. Match anything that is not a digit:
\D
pf2

Partial preview of the text

Download Regular Expression Examples and Questions - Prof. Stephany S. Coffman-Wolph and more Assignments Computer Science in PDF only on Docsity!

Regular Expression Example Questions: Write Regular expressions to do the following:

  1. Match 0 or more characters: .*
  2. Match 1 or more characters: .+
  3. Match any sequence of letters in upper or lower case, but there must be at least 1 letter: [a-zA-Z]+
  4. Match any letter that is the range of a to d (lowercase): [a-d]
  5. Match any character that is not in the range a to d: [^a-d]
  6. Match any sequence of at least one word character: \w+
  7. Match a sequence of exactly four word characters: \w{4} or \w\w\w\w or [a-zA-Z_]{4}
  8. Match any digit: \d or [0-9]
  9. Match anything that is not a digit: \D
  1. Match exactly three digits: \d{3} or \d\d\d or [0-9]
  2. Match a sequence that starts with the letter d (could be uppercase or lowercase), ends with the letter e (lowercase), and otherwise could contain any letters (uppercase or lowercase) in- between: (d|D)[a-zA-Z]*e
  3. Recognize the word grey or gray: (grey)|(gray) or gr(e|a)y
  4. Recognize either 101 or 1001: (101)|(1001) or 10(0?)
  5. Recognize a number that begins with a 1, ends with a 1, and has at least 5 0’s (in the middle): 10{5}