Subexpressions in Regular Expressions
It is not unusual to have to look for patterns that contain a smaller sequence of characters in a specified order. Regexes to do this are constructed by using subexpressions - a sequence wrapped in parentheses. For example, the pattern (\d{1,3}\.){3}(\d{1,3}) matches 127.0.0.1 and 66.249.67.206 but not 127.0.0 or abc.lmn.pqr.xyz. This expression merits a further explanation
- The part
(\d{1,3}\.)is a subexpression. It indicates that a pattern bearing one of the three forms ddd., dd. or d. is expected. - The part
{3}indicates that the above pattern must repeat three times - The part
(d{1,3})is another subexpression. It indicates that one fo the patterns ddd, dd or d is required.
The three parts together makeup our IP address verification pattern. We hasten to add that this pattern is far from correct. It will happily accept invalid IP addresses such as 789.000.456.999. A more complete IP address verification pattern is discussed here.
Regular Expression Sandbox
Model
Data
Do not wrap the model expression in a /.../ pair. The characters
Do not wrap the model expression in a /.../ pair. The characters
^$.?*!+:=()[]{}|\\ must be escaped - except when then occur inside a character class. Invalid characters will be grayed out.| Result | Left Text | Match | Right Text |
|---|---|---|---|
Download