Lesson 6

Position Matching

You’ve now learned how to match all sorts of characters in all sorts of combinations and repetitions and in any location within text. However, it is sometimes necessary to match at specific locations within a block of text, and this requires position matching, which is explained in this lesson.

Using Boundaries

Position matching is used to specify where within a string of text a match should occur. To understand the need for position matching, consider the following example:

Text

The cat scattered his food all over the room.

RegEx

cat

Result

The cat scattered his food all over the room.

Analysis

The pattern cat matches all occurrences of cat, even cat within ...

Get Learning Regular Expressions, First Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.