普通的正则函数是非常简洁的,但当它们变长时,它们就变得越来越难理解。
默认情况下,它们没有注释,而空白是重要的。
然后是大量的符号和反斜杠转义,不遵循任何可识别的系统。(?<=) (?P<>) .?? \N \p{} \k<> \g'' 等等。
而且,在不同的regex实现之间存在各种不一致,这是造成混乱的完美秘诀。
Rulex用一种新的、更简单的语法解决了这些问题。
- 它对空格不敏感,并允许注释
- 文本必须出现在引号中。这使得表达式更长,但也更容易阅读。
- 没有反斜杠转义
- 非捕获组是默认的
- 语法更一致
Rulex目前与PCRE、JavaScript、Java、.NET、Python、Ruby和Rust兼容。在编译过程中,必须要指定搜索结果,所以rulex可以确保产生的搜索结果在目标搜索结果引擎上能正常工作。
对JavaScript用户的重要提示。不要忘记启用u标志。这是支持Unicode的必要条件。所有其他主要的 regex 引擎默认都支持 Unicode。
案例:
<strong>String</strong> 'hello world' <strong>hello world</strong>
<strong>Lazy repetition</strong> 'hello'{1,5} <strong>(?:hello){1,5}?</strong> 'hello'* <strong>(?:hello)*?</strong> 'hello'+ <strong>(?:hello)+?</strong>
<strong>Greedy repetition</strong> 'hello'{1,5} greedy <strong>(?:hello){1,5}</strong> 'hello'* greedy <strong>(?:hello)*</strong> 'hello'+ greedy <strong>(?:hello)+</strong>
<strong>Alternation</strong> 'hello' | 'world' <strong>hello|world</strong>
<strong>Character classes</strong> ['aeiou'] <strong>[aeiou]</strong> ['p'-'s'] <strong>[p-s]
|
它不受任何正则表达式库的支持。您只需指定正则表达式风格(例如Rust,如果您使用的是 Rust 正则表达式箱),它会输出一个正则表达式,然后您可以将其传递给任何正则表达式引擎:
let my_regex = regex::Regex::new( rulex::Rulex::compile( r#"['tT'] 'est' '!'+"#, rulex::options::CompileOptions { flavor: rulex::options::RegexFlavor::Rust, ..Default::default() }, ).unwrap(), ).unwrap();
|
但是如果在编译时知道正则表达式,推荐使用 rulex-macro crate,它更方便,不添加运行时依赖,甚至在编译时显示诊断:
let my_regex = regex::Regex::new( rulex!(r#"['tT'] 'est' '!'+"#), ) .unwrap();
|