re2 0.1.0 copy "re2: ^0.1.0" to clipboard
re2: ^0.1.0 copied to clipboard

Linear-time regular expressions for Dart via Google's RE2 over FFI. Immune to catastrophic backtracking (ReDoS).

example/re2_example.dart

import 'package:re2/re2.dart';

// Running a pattern that comes from outside your code, for example a
// search filter a user typed, is where a backtracking engine can be made
// to hang. RE2 matches in linear time, so the same input stays fast.
void main() {
  final userPattern = r'([a-zA-Z0-9._-]+)@([a-zA-Z0-9.-]+)';
  final re = Re2(userPattern);
  try {
    final text = 'reach me at bob@example.com or alice@dartlang.org';
    for (final match in re.allMatches(text)) {
      print('${match.group(1)} at ${match.group(2)}');
    }

    // A pattern that needs backreferences or lookaround is rejected at
    // construction, so you learn about it before matching, not during.
    try {
      Re2(r'(\w+)\1'); // backreference
    } on FormatException catch (e) {
      print('rejected unsupported pattern: ${e.message}');
    }
  } finally {
    re.dispose();
  }
}
0
likes
0
points
79
downloads

Publisher

verified publisherdeveloperyusuf.com

Weekly Downloads

Linear-time regular expressions for Dart via Google's RE2 over FFI. Immune to catastrophic backtracking (ReDoS).

Repository (GitHub)
View/report issues

Topics

#regex #re2 #security #ffi #performance

License

unknown (license)

Dependencies

code_assets, ffi, hooks, native_toolchain_c

More

Packages that depend on re2