derivre

Derivative-based regular expression engine for Rust

MIT License

Stars
4
Committers
3

Derivative based regex matcher

For basic introduction see Regular-expression derivatives reexamined.

For extensions, see Derivative Based Nonbacktracking Real-World Regex Matching with Backtracking Semantics and Derivative Based Extended Regular Expression Matching Supporting Intersection, Complement and Lookarounds and the sbre implementation of it.

Usage

This library uses regex-syntax for regular expression parsing. This means that currently there is no surface syntax for & and ~ operators (but the library supports it).

The library only checks if the regex matches the string from the beginning (it doesn't search for it, or in other words there's an implied \A at the beginning).

let mut rx = Regex::new("[ab]c").unwrap();
assert!(rx.is_match("ac"));
assert!(rx.is_match("bc"));
assert!(!rx.is_match("xxac"));
assert!(!rx.is_match("acxx"));

The library supports a single look-ahead at the end of the regex, written as A(?P<stop>B) where A and B are regexes without any look-arounds. You can get the length of the string matching B upon successful match.

// the syntax in other libraries would be: r"\A[abx]*(?=[xq]*y)"
let mut rx = Regex::new("[abx]*(?P<stop>[xq]*y)").unwrap();
assert!(rx.lookahead_len("axxxxxy") == Some(1));
assert!(rx.lookahead_len("axxxxxqqqy") == Some(4));
assert!(rx.lookahead_len("axxxxxqqq") == None);
assert!(rx.lookahead_len("ccqy") == None);

Code map

In recommended reading order:

The rest:

TODO

  • more simplification rules from sbre
  • benchmarks
  • extend regex-syntax for & and ~ operators
  • add & valid-utf8 if there is negation somewhere
  • either make derivative() non-recursive (mk_*() already are?) or limit the regex depth
  • implement relevance check for & and ~ operators; see symbolic derivatives
  • add .forced_byte() method on state descriptor

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.