regex_builder 0.1.0+5 copy "regex_builder: ^0.1.0+5" to clipboard
regex_builder: ^0.1.0+5 copied to clipboard

outdated

A declarative builder for Regex.

A declarative way to write regular expressions in Dart. This package was inspired by Swift RegexBuilder.

Important: This package is still in development and could change until the first stable release.

Features #

RegexBuilder conforms with RegExp, thus all methods and properties are available to use.

The following example finds all matches of word in a string.

final regex = RegexBuilder([
  Group([OneOrMore(CharacterSet.word)])
]);

final str = 'Parse my string';
Iterable<RegExpMatch> matches = regex.allMatches(str);
for (final m in matches) {
  print(m[0]);
}

The output of the example is:

Parse
my
string

Components #

A RegexBuilder is composed of a list of components. The following components are available:

Literal #

A literal is a string of characters that must be matched exactly.

const RegexComponent abc = Literal('abc'); // This represents the pattern 'abc'

Wildcards #

A wildcard is a character that matches any character.

const RegexComponent anyCharacter = AnyCharacter(); // This represents the pattern '.'

Alternation #

An alternation is a list of components that could match any of them.

const RegexComponent carOrDog = ChoiceOf([
  Literal('cat'),
  Literal('dog'),
]); // This represents the pattern 'cat|dog'

Quantifiers #

Quantifiers are used to specify how many times a component should be matched.

ZeroOrMore

Matches zero or one time.

const RegexComponent zeroOrMore = ZeroOrMore(Literal('a')); // This represents the pattern 'a*'

OneOrMore

Matches one or more times.

const RegexComponent oneOrMore = OneOrMore(Literal('a')); // This represents the pattern 'a+'

Optionally

Matches zero or one time.

const RegexComponent optionally = Optionally(Literal('a')); // This represents the pattern 'a?'

Repeat

Matches a specific number of times.

const RegexComponent repeatAtLeast = Repeat(
  Literal('a'),
  range: RepeatRange.atLeast(3),
); // This represents the pattern 'a{3,}'

const RegexComponent repeatAtMost = Repeat(
  Literal('a'),
  range: RepeatRange.atMost(3),
); // This represents the pattern 'a{0,3}'

const RegexComponent repeatExactly = Repeat(
  Literal('a'),
  range: RepeatRange.exactly(3),
); // This represents the pattern 'a{3}'

const RegexComponent repeatBetween = Repeat(
  Literal('a'),
  range: RepeatRange.between(3, 5),
); // This represents the pattern 'a{3,5}'

CharacterSet #

Group #

A group is a list of components that must be matched together. By default, a group is non-capturing.

const RegexComponent group = Group([
  Literal('a'),
  Literal('b'),
]); // This represents the pattern '(?:ab)'

Capture

A capture group is a list of components that must be matched together and will be captured.

const RegexComponent unnamedCapture = Group([
  Literal('a'),
  Literal('b'),
], behavior: GroupBehavior.capture()); // This represents the pattern '(ab)'

const RegexComponent namedCapture = Group([
  Literal('a'),
  Literal('b'),
], behavior: GroupBehavior.capture('group_name')); // This represents the pattern '(?<group_name>ab)'

Anchor #

An anchor is a component that matches the beginning or end of a string.

const RegexComponent startOfSLine = Anchor.startOfLine; // This represents the pattern '^'
const RegexComponent endOfLine = Anchor.endOfLine; // This represents the pattern '$'
9
likes
0
points
1
downloads

Publisher

verified publisherailton.dev

Weekly Downloads

A declarative builder for Regex.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta

More

Packages that depend on regex_builder