{1,10}: Make Your Own Super Powers
The special characters you just learned about are some of the most commonly used characters in regular expressions, but there’s more. If ?
, +
, and *
aren’t specific enough, you can define your own lengths using curly braces. Remember the yearPattern
example that matches a sequence of exactly four digits? You can use the curly braces in three ways:
{n}
matches the preceding character exactly n times.
{n,}
matches the preceding character at least n times.
{m,n}
matches the preceding character at least m times, ...
Get Learning to Program 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.