Wednesday, January 21, 2009

Grouping

You can form groups in regular expressions and work with the results of groups. Let’s return to our initial example string. A group is simply formed by putting round brackets.

([0-9]*)([^0-9]*)

199 mountains reach the skies.


The first group will contain the 199 and the second group will contain the rest of the string. If you are using the RegEx Coach you may highlight the groups by clicking on 1 or 2 at the bottom.





If you do not need the result of a group. You can put a :? at the beginning. We will learn how to work with the results of groups later.

(:?[0-9]*)(:?[^0-9]*)

No comments:

Post a Comment