verbal_expressions 0.4.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 50

Pub Package Build Status Coverage Status Github Issues

verbal_expressions

A library for Dart developers that helps to construct difficult regular expressions.

Dart package info is here: https://pub.dartlang.org/packages/verbal_expressions

Quick start

  var regex = VerbalExpression()
   ..startOfLine()
   ..then("http").maybe("s")
   ..then("://")
   ..maybe("www.").anythingBut(" ")
   ..endOfLine();

  // Create an example URL
  String url = "https://www.google.com";

  // Use VerbalExpression's hasMatch() method to test if the entire string matches the regex
  regex.hasMatch(url); //True

  regex.toString();   // Outputs the regex used: ^http(s)?\\:\\/\\/(www\\.)?([^\\ ]*)\$

  var regex = VerbalExpression()..startOfLine()..then("abc")..or("def");

  var testString = "defzzz";
  //Use VerbalExpression's hasMatch() method to test if parts if the string match the regex
  regex.hasMatch(testString);   // true

Feel free to use any predefined char groups:

  var regex = VerbalExpression()
	  ..wordChar()..nonWordChar()
	  ..space()..nonSpace()
	  ..digit()..nonDigit();

Define captures:

  var expression = VerbalExpression()
   ..find("a")
   ..beginCapture()..find("b")..anything()..endCapture()
   ..then("cd");
   
  RegExp regex = expression.toRegExp();

  var match = regex.firstMatch(text);
  print(match.group(0)); // returns "abcd"
  print(match.group(1)); // returns "b"

Examples

More examples are in example file

Features and bugs

Please find feature requests and bugs at the issue tracker.

Other implementations

You can view all implementations on VerbalExpressions.github.io

[ Javascript - PHP - Python - C# - Objective-C - Ruby - Groovy - Haskell - C++ - ... (moarr) ]

Changelog

0.4.0

  • Updated to Dart 2

0.3.1

  • Added .analysis_options and fixed common problems

0.3.0

  • breaking change: do not capture groups if it is not set explicitly using beginCapture/endCapture. More details
  • fixed group capturing
  • added lazy quantifiers
  • added Macintosh line break

0.2.0

  • breaking change: builder pattern approach replaced with method cascades. More details
  • set global search by default

0.1.0

  • Initial version, created by Ne4istb

example/verbal_expressions_example.dart

library verbal_expressions_example.example;

import 'package:verbal_expressions/verbal_expressions.dart';

main() {
  var result = matchUrl('https://www.google.com'); // true
  // ...

  String phoneWithSpace = "+097 234 243";
  result = matchTelephoneNumber(phoneWithSpace); //true
  // ...

  String phoneWithoutSpace = "+097234243";
  result = matchTelephoneNumber(phoneWithoutSpace); // true
  // ...

  String phoneWithDash = "+097-234-243";
  result = matchTelephoneNumber(phoneWithDash); // true
  // ...

  var domain = getDomain('https://www.google.com');
  print(domain); // .com

  domain = getDomain('http://ru.wikipedia.org/wiki/Dart');
  print(domain); // .org

  var expression = VerbalExpression()
    ..find('dog')
    ..stopAtFirst()
    ..withAnyCase();

  var testString = expression.replace('Replace first DoG in the sentence but do not touch second dog', 'cat');

  print(testString); // Replace first cat in the sentence but do not touch second dog
}

String getDomain(String url) {
  var expression = VerbalExpression()
    ..startOfLine()
    ..then("http")
    ..maybe("s")
    ..then("://")
    ..maybe("www.")
    ..anythingBut(" ")
    ..beginCapture()
    ..then('.')
    ..anythingBut('/')
    ..endCapture()
    ..anything()
    ..endOfLine();

  return expression.toRegExp().firstMatch(url).group(1);
}

bool matchTelephoneNumber(String number) {
  var regex = VerbalExpression()
    ..startOfLine()
    ..then("+")
    ..beginCapture()
    ..range([Range('0', '9')])
    ..count(3)
    ..maybe("-")..maybe(" ")
    ..endCapture()
    ..count(3)
    ..endOfLine();

  return regex.hasMatch(number);
}

bool matchUrl(String url) {
  var regex = VerbalExpression()
    ..startOfLine()
    ..then("http")
    ..maybe("s")
    ..then("://")
    ..maybe("www.")
    ..anythingBut(" ")
    ..endOfLine();

  return regex.hasMatch(url);
}

Use this package as a library

1. Depend on it

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


dependencies:
  verbal_expressions: ^0.4.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:verbal_expressions/verbal_expressions.dart';
  
Version Uploaded Documentation Archive
0.4.0 Aug 12, 2018 Go to the documentation of verbal_expressions 0.4.0 Download verbal_expressions 0.4.0 archive
0.3.1 Jan 14, 2017 Go to the documentation of verbal_expressions 0.3.1 Download verbal_expressions 0.3.1 archive
0.3.0 Mar 10, 2016 Go to the documentation of verbal_expressions 0.3.0 Download verbal_expressions 0.3.0 archive
0.2.0 Jan 19, 2016 Go to the documentation of verbal_expressions 0.2.0 Download verbal_expressions 0.2.0 archive
0.1.0 Jan 16, 2016 Go to the documentation of verbal_expressions 0.1.0 Download verbal_expressions 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Sep 18, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.3

Platforms

Detected platforms: Flutter, web, other

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

Suggestions

Format lib/src/range.dart.

Run dartfmt to format lib/src/range.dart.

Format lib/src/verbal_expression.dart.

Run dartfmt to format lib/src/verbal_expression.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test ^1.3.0