recursive_regex 0.1.3

recursive_regex #

An implementation of Dart's RegExp class that isolates delimited blocks of text and applies the delimited pattern to each block separately.

The [RecursiveRegex] class implements Dart's [RegExp] class. As such, it has all of the same methods as [RegExp] and can be used interchangeably with [RegExp]s.

Usage #

import 'package:recursive_regex/recursive_regex.dart';

[RecursiveRegex] has 2 required parameters, [startDelimiter] and [endDelimiter]. Both accept a [RegExp], must not be null, and must not have identical patterns.

RegExp regex = RecursiveRegex(
  startDelimiter: RegExp(r'<'),
  endDelimiter: RegExp(r'>'),
);

This [RecursiveRegex], if applied to a string, would capture every block of text delimited by the < and > characters.

String input = '<a<b<c><d>>>';

regex.allMatches(input); // ['<a<b<c><d>>>']

By default, only top-level blocks of delimited text are matched. To match every block of delimited text, nested or not, global can be flagged true.

String input = '<a<b<c><d>>>';

RegExp regex = RecursiveRegex(
  startDelimiter: RegExp(r'<'),
  endDelimiter: RegExp(r'>'),
  global: true,
);

regex.allMatches(input); //['<c>', '<d>', '<b<c><d>>', '<a<b<c><d>>>']

Delimited blocks of text are matched in the order that they're closed.

Note: [RecursiveRegex] is not optimized for parsing strings that do not contain nested blocks of delimited text, [RegExp] would be more efficient if you know the string doesn't contain any nested delimiters.

Methods #

[RecursiveRegex] has the same methods as [RegExp] (@override methods), plus a few more.

/// Returns the first match found in [input].
@override
RegExpMatch firstMatch(String input);

/// Searches [input] for the match found at [index].
RegExpMatch nthMatch(int index, String input, {bool reverse = false});

/// Returns the last match found in [input].
RegExpMatch lastMatch(String input);

/// Returns a list of every match found in [input] after [start].
@override
List<RegExpMatch> allMatches(String input, [int start = 0]);

/// Returns a list of the matches found in [input].
List<RegExpMatch> getMatches(String input, {
  int start = 0, int stop, bool reverse = false,
});

/// Returns `true` if [input] contains a match, otherwise returns `false`.
@override
bool hasMatch(String input);

/// Returns the first substring match found in [input].
@override
String stringMatch(String input);

/// Returns the list of substring matches found in [input].
List<String> stringMatches(String input, {
  int start = 0, int stop, bool reverse = false,
});

/// Match the delimited pattern against the start of [string].
@override
Match matchAsPrefix(String string, [int start = 0]);

Behind the scenes, firstMatch(), nthMatch(), lastMatch(), and allMatches() return results from getMatches().

getMatches(), will identify every block of delimited text, and will apply the delimited pattern to each block seperately. The pattern will only be applied to the blocks being returned, all others will be ignored.

If getMatches() [reverse] parameter is true, blocks of delimited text will be identified in [input] from the bottom-up, as such, calling lastMatch() is significantly more efficient than calling allMatches().last.

getMatches() [start] and [stop] parameters set the range of matches that should be returned. Matches with indexes that occur before [start] and after [stop] will be ignored. nthMatches(3, input) would call getMatches(input, start: 3, stop: 3).

Capture Groups #

The block of text captured between the delimiters can be captured in a named group by providing [RecursiveRegex] with a [captureGroupName].

String input = '<!ELEMENT [ some markup ]>';

RegExp regex = RecursiveRegex(
  startDelimiter: RegExp(r'<!(?<type>\w*)\s*\['),
  endDelimiter: RegExp(r']>'),
  captureGroupName: 'value',
);

print(regex.firstMatch(input).namedGroup('value')); // ' some markup '

// Groups named within the delimiters can also be called.
print(regex.firstMatch(input).namedGroup('type')); // 'ELEMENT'

[0.1.1 - 0.1.3] - August 10, 2019

  • Bug fixes

[0.1.0] - August 8, 2019

  • Initial Release

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  recursive_regex: ^0.1.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:recursive_regex/recursive_regex.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
21
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
58
Learn more about scoring.

We analyzed this package on Aug 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:recursive_regex/recursive_regex.dart.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and recursive_regex.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
meta ^1.1.7 1.1.7
Dev dependencies
test ^1.6.5