As I mentioned previously, parentheses can be used to group choices, or a pattern that should be quantified. When using them from JavaScript, PHP, or other RegExp supporting language, anything found within a group will also be returned as a result. So for example, in the following expression the results returned would be abba, aba and bb when applied to abaaabbaa.
a(b){1,2}a
To stop the grouped results being returned, they can be made passive by using ?: at the start causes it to become a non-capturing group (e.g. a(?:a|b)a will match, but will only return the overall match if there is one). A better example would be to consider the following paragraph of text:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec lobortis eros in erat. Sed id tortor id est nonummy varius. Duis odio sapien, cursus sit amet, tincidunt et, imperdiet vitae, mi. Suspendisse ut velit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin ac nibh eu nisi porta venenatis. Ut scelerisque dignissim nisi.
Proin ut velit. Integer porttitor. Morbi ultrices, enim vitae vestibulum vehicula, lorem enim porta nisl, a cursus est ipsum ut nibh. Fusce vulputate, leo in accumsan viverra, felis libero tincidunt sem, ac luctus tortor enim vel nulla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse mattis dolor id augue ultrices consequat. Integer sodales. Fusce pulvinar viverra libero.
If we then performed (?in){1} on the string it would find 7 matches.
Very similar to groups, is that of ranges. A range can be used to specify a custom range of characters, numbers, or symbols to match.
[0-9]*([a-zA-Z\s]+)+
The above example uses a special character (which will be described in more detail) later to specify that whitespace is allowed to be matched. The pattern will look for 0 or more digits followed by 1 or more words. This can be used to validate an address, for example it will match 4 Wheel Drive as it starts with a digit, and is then followed by 2 words.[a-zA-Z\s] is used to specify a range of characters from a-z and A-Z are allowed, as well as any white-space (\s).
[^g-z]+
The above example uses a caret (^, sometimes referred to as a hat due to how it is used in mathematics) to signify a negation. This means that it will match 1 or more of any character that is not in the range of lowercase g to z. The caret can only be used at the start of the pattern, and cannot be used in the middle (the caret is also used as a way of signifying the pattern must be matched at the start of the string).













