Report a bug If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.

std.regex

Regular expressions are a commonly used method of pattern matching
on strings, with regex being a catchy word for a pattern in this domain
specific language. Typical problems usually solved by regular expressions
include validation of user input and the ubiquitous find & replace
in text processing utilities.

The general usage guideline is to keep regex complexity on the side of simplicity,
as its capabilities reside in purely character-level manipulation.
As such it's ill-suited for tasks involving higher level invariants
like matching an integer number bounded in an [a,b] interval.
Checks of this sort of are better addressed by additional post-processing.

The basic syntax shouldn't surprise experienced users of regular expressions.
For an introduction to std.regex see a
short tour of the module API
and its abilities.

There are other web resources on regular expressions to help newcomers,
and a good reference with tutorial
can easily be found.

This library uses a remarkably common ECMAScript syntax flavor
with the following extensions:

All matches returned by pattern matching functionality in this library
are slices of the original input. The notable exception is the replace
family of functions that generate a new string from the input.

In cases where producing the replacement is the ultimate goal
replaceFirstInto and replaceAllInto could come in handy
as functions that avoid allocations even for replacement.

Delegating the kind of operation
to "g" flag is soon to be phased out along with the
ability to choose the exact matching scheme. The choice of
matching scheme to use depends highly on the pattern kind and
can done automatically on case by case basis.

Delegating the kind of operation
to "g" flag is soon to be phased out along with the
ability to choose the exact matching scheme. The choice of
matching scheme to use depends highly on the pattern kind and
can done automatically on case by case basis.

This is a general replacement tool that construct a new string by replacing
matches of pattern re in the input. Unlike the other overload
there is no format string instead captures are passed to
to a user-defined functor fun that returns a new string
to use as replacement.

This version replaces the first match in input,
see replaceAll to replace the all of the matches.

Returns:

A new string of the same type as input with all matches
replaced by return values of fun. If no matches found
returns the input itself.

A variation on replaceFirst that instead of allocating a new string
on each call outputs the result piece-wise to the sink. In particular
this enables efficient construction of a final output incrementally.

Like in replaceFirst family of functions there is an overload
for the substitution guided by the format string
and the one with the user defined callback.

This is a general replacement tool that construct a new string by replacing
matches of pattern re in the input. Unlike the other overload
there is no format string instead captures are passed to
to a user-defined functor fun that returns a new string
to use as replacement.

This version replaces all of the matches found in input,
see replaceFirst to replace the first match only.

Returns:

A new string of the same type as input with all matches
replaced by return values of fun. If no matches found
returns the input itself.

A variation on replaceAll that instead of allocating a new string
on each call outputs the result piece-wise to the sink. In particular
this enables efficient construction of a final output incrementally.

As with replaceAll there are 2 overloads - one with a format string,
the other one with a user defined functor.