Thursday, January 22, 2009

Anchoring

It is possible to anchor your regexp to each end or both ends of the string. To make sure the match starts with the beginning of the string you need to use the ^ anchor. Remember that the ^ within a character class [] has a different meaning.

^[19]+

199 mountains reach the skies.

^[19]+ will not match

mountains reach 199 skies.

Because the 1 or 9 needs to be at the beginning of the string to make the regexp match. Try removing the anchor an see what happens.

Using the $ at the end of your regexp anchors it to the end of the string.

skies.$

mountains reach the 199 skies.

[1-9].*$

mountains reach the 199 skies.

^.*(1.+9).*$

mountains reach the 199 skies.
Form a group around 1 anything 9 and anchor it to both ends of the string.

No comments:

Post a Comment