ArticleslgStudy

computer science

RE2 (software)

RE2 (software) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand RE2 (software) rather than just read about it. In short: RE2 is a C++ software library which implements a regular expression engine. It uses finite-state machines, in contrast to most other regular expression libraries.

Key takeaways

  • RE2 (software) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect RE2 (software) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of RE2 (software) from memory before moving on to harder problems.

Reference excerpt

RE2 is a C++ software library which implements a regular expression engine. It uses finite-state machines, in contrast to most other regular expression libraries. RE2 requires a minimum C++ version of C++17, and uses the Abseil library by Google. RE2 was implemented by Google and Google uses RE2 for Google products. RE2 uses an "on-the-fly" deterministic finite-state automaton algorithm based on Ken Thompson's Plan 9 grep. It is designed to avoid ReDoS (regex denial of service) attacks.

Comparison to PCRE RE2 performs comparably to Perl Compatible Regular Expressions (PCRE). For certain regular expression operators like | (the operator for alternation or logical disjunction) it is superior to PCRE. Unlike PCRE, which supports features such as lookarounds, backreferences and recursion, RE2 is only able to recognize regular languages due to its construction using the Thompson DFA algorithm. It is also slightly slower than PCRE for parenthetic capturing operations. PCRE can use a large recursive stack with corresponding high memory usage and result in exponential runtime on certain patterns. In contrast, RE2 uses a fixed stack size and guarantees that its runtime increases linearly (not exponentially) with the size of the input. The maximum memory allocated with RE2 is configurable. This can make it more suitable for use in server applications, which require boundaries on memory usage and computational time.

Adoption RE2 is available to users of Google Docs and Google Sheets. Google Sheets supports RE2 except Unicode character class matching. RegexExtract does not use grouping.

Example Here is an example of using re2 against a potential ReDoS (regular expression denial of service) attack.

Related libraries RE2 comes with a built-in Python wrapper, available on Python Package Index (PyPI) as google-re2. The built-in regexp package in Go uses the same patterns and implementation as RE2, though it is written in Go. This is unsurprising, given Go's common staff from the Plan 9 team. The RE2 algorithm has been rewritten in Rust as the package regex. CloudFlare's web application firewall uses this package because the RE2 algorithm is immune to ReDoS. Russ Cox also wrote RE1, an earlier regular expression based on a bytecode interpreter. OpenResty uses a RE1 fork called "sregex". There is an official Java binding, called RE2J (com.google.re2j). The following languages have unofficial bindings:

C (cre2) D (re2d) Erlang (re2) Inferno (inferno-re2) JavaScript (RE2JS), a port of RE2J to JavaScript Node.js (node-re2) OCaml (re2) Perl (re-engine-RE2) R (re2) Ruby (re2) WebAssembly (re2-wasm)

See also

Comparison of regular expression engines

References

Worked examples

Example 1 — a first encounter with RE2 (software)

Start with the simplest possible case. Write down what RE2 (software) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to RE2 (software) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about RE2 (software) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of RE2 (software)

In research
RE2 (software) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses RE2 (software) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
RE2 (software) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Regular expressions, Software using the BSD license, so understanding it makes those chapters shorter.
In everyday life
Look for RE2 (software) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study RE2 (software) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what RE2 (software) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain RE2 (software) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is RE2 (software) in simple terms?

RE2 is a C++ software library which implements a regular expression engine. It uses finite-state machines, in contrast to most other regular expression libraries.

Why does RE2 (software) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study RE2 (software)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on RE2 (software).

Tags

  • Regular expressions
  • Software using the BSD license

Keep exploring