Question mark (?)
The question mark (?) matches the preceding character zero or one times.
For example, 10?
matches the following:
- 1
- 10
Example
Match an IP address with one or two digits in the last section.
For example, 216.239.32.\d\d?
matches the following:
- 216.239.32.2
- 216.239.32.34
This example uses the backslash to escape the decimal, and it uses \d
to match any digit.
Plus sign (+)
The plus sign (+) matches the preceding character one or more times.
For example, 10+
matches the following:
- 10
- 100
- 1000
- etc.
Example
Match an IP address with one or more digits in the last section.
For example, 216.239.32.\d+
matches the following:
- 216.239.32.2
- 216.239.32.34
- 216.239.32.567
This example uses the backslash to escape the decimal, and it uses \d
to match any digit.
Asterisk (*)
The asterisk or star matches the preceding character zero or more times.
For example, 10*
matches the following:
- 1
- 10
- 100
- 1000
- etc.
Example
Match an IP address with zero or more digits in the last section.
For example, 216.239.32.\d*
matches the following:
- 216.239.32.
- 216.239.32.2
- 216.239.32.34
- 216.239.32.567
This example uses the backslash to escape the decimal, and it uses \d
to match any digit.
If you need to match more than just the preceding item, you can combine the asterisk () with the dot (
.
). The dot matches any preceding item, and then the asterisk matches that item zero or more times, which lets you match things like all URIs that begin and end with the same characters, regardless of how many characters are in between. For example, /mens/.*html
matches the following:
- /mens/shirts/oxford.html
- /mens/shirts/oxford/shortsleeve.html