Questions tagged [re2]

RE2 is a fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, Python and Go. It is a C++ library.

RE2 is a fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, Python and Go. It is a C++ library, released under the BSD License.

Backtracking engines are typically full of features and convenient syntactic sugar but can be forced into taking exponential amounts of time on even small inputs. RE2 uses automata theory to guarantee that regular expression searches run in time linear in the size of the input. RE2 implements memory limits, so that searches can be constrained to a fixed amount of memory. RE2 is engineered to use a small fixed C++ stack footprint no matter what inputs or regular expressions it must process; thus RE2 is useful in multithreaded environments where thread stacks cannot grow arbitrarily large.

When RE2 was released, Russ Cox the main author of RE2, published a series of articles on the CS theory and real world implementation of regular expressions. Including some in depth benchmarks of RE2 vs competing regex libraries.

Bindings for some scripting languages languages exist for Python and Ruby.

Supported syntax can be found here.

382 questions
6
votes
1 answer

Why implement a different regex engine (e.g. PCRE) as a pragma?

I'm curious about the best practices for using a different regex engine in place of the default Perl one and why the modules I've seen are pragmas and not a more traditional OO/procedural interface. I was wondering why that is. I've seen a handful…
Greg Nisbet
  • 6,710
  • 3
  • 25
  • 65
6
votes
1 answer

Why re2 result different from re module in Python?

I try to use re2. import re print re.search('cde', 'abcdefg').group(0) Result: cde But re2 result is different import re2 print re2.search('cde', 'abcdefg').group(0) Result: 1 Traceback (most recent call last): File "", line 1, in…
Puffin GDI
  • 1,702
  • 5
  • 27
  • 37
5
votes
1 answer

pyre2 is slower than built-in re module?

When using pyre2 (https://github.com/axiak/pyre2), I encountered a performance problem (matching time). I have three programs: pure Python using built-in re module: https://gist.github.com/1873402 Python using Pyre2:…
ukessi
  • 1,381
  • 1
  • 10
  • 15
5
votes
2 answers

re2 library loading

I am using the following code to test google's RE2 library int main() { int r = RE2::FullMatch("hello", "h.*o"); cout <<" match = " << r << endl; return 0; } I am compling using the following command - g++ -lre2 -o retest retest.cc It is…
ocwirk
  • 1,079
  • 1
  • 15
  • 35
5
votes
1 answer

Golang regex to extract values inside parantheses and ignore inner parantheses in any

I have the following example of key=value pairs as one line string start=("a", "b") and between=("range(2019, max, to=\"le\")") and end=("a", "b") Using regex in golang I want to extract the key=value pairs as below start=("a",…
Hussain
  • 436
  • 3
  • 8
5
votes
1 answer

Simultaneously Matching Multiple Regular Expressions with Google RE2

I'm attempting to match many (500+) regular expressions quickly using Google's RE2 Library, as I'd like to get similar results to this whitepaper. I'd like to use RE2-m on page 13. From what I've seen online, the Set interface is the way to go,…
EnnFour
  • 323
  • 2
  • 8
5
votes
0 answers

What is the correct regex syntax in Google Cloud Firestore Security Rules?

EDIT: After viewing the answer at https://stackoverflow.com/a/44876864/6792075, I still don't know why it should be necessary to double escape the period, with '\\.', especially because the documentation clearly shows that '\.' is the expected…
5
votes
1 answer

Why is node's backtracking regex faster than RE2 in this example

I need to accept Regex from users--crazy, I know. The Google RE2 regular expression parser is safer than PCRE-based ones since it doesn't use backtracking, thus preventing catastrophic backtracking, infinite loops and general mayhem. It's…
Donald E. Foss
  • 163
  • 1
  • 9
5
votes
4 answers

pip install re2 gives an error

I'm trying to download re2 by the command pip install re2 and it gives me this: Collecting re2 Using cached re2-0.2.22.tar.gz Building wheels for collected packages: re2 Running setup.py bdist_wheel for re2 ... error Complete output from…
johndoe_asking
  • 141
  • 1
  • 3
  • 14
5
votes
1 answer

Negate match for word in the beginning of string in RE2 syntax?

Let's say that I have following strings: mail to tel:+358123456 http://www.google.fi mailto:foo@bar.fi hello world telephone elephant penny link owl How can I find only strings that do not start with 'tel:', 'http://' and 'mailto:' in RE2…
Jehki
  • 67
  • 1
  • 4
5
votes
2 answers

issue with regexp with nested groups in golang

Consider the following toy example. I want to match in Go a name with a regexp where the name is sequences of letters a separated by single #, so a#a#aaa is valid, but a# or a##a are not. I can code the regexp in the following two ways: r1 :=…
Igor Bukanov
  • 4,636
  • 3
  • 16
  • 23
4
votes
2 answers

RE2 and UTF16 (or UCS-2)

RE2 is great. Fast and deterministic. However, it supports only UTF8. My strings are natively UTF16, and converting back and forth would kill performance. How difficult would it be to implement native UTF16 capability in RE2? How difficult would it…
MustafaM
  • 493
  • 1
  • 4
  • 14
4
votes
7 answers

Get a match when there are duplicate letters in a string

I have a list of inputs in google sheets, Input Desired Output "To demonstrate only not an input" The repeated letters Outdoors Match o dog No Match step No Match bee Match e Chessboard Match s Cookbooks Match o, k How do I…
Osm
  • 2,699
  • 2
  • 4
  • 26
4
votes
1 answer

regexp: multiline, non-greedy match until optional string

Using Go's regexp, I'm trying to extract a predefined set of ordered key-value (multiline) pairs whose last element may be optional from a raw text, e.g., Key1: SomeValue1 MoreValue1 Key2: SomeValue2 MoreValue2 OptionalKey3: SomeValue3 …
Joseph
  • 150
  • 8
4
votes
1 answer

How are named capture groups used in RE2 regexps?

On this page http://swtch.com/~rsc/regexp/regexp3.html it says that RE2 supports named expressions. RE2 supports Python-style named captures (?Pexpr), but not the alternate syntaxes (?expr) and (?'name'expr) used by .NET and …
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114
1
2
3
25 26