Skip to main content
Version: 3.2.17

Regular Expressions

A regular expression (regex) is a pattern language used to match, search, and replace text. In Reqable, regex is widely used in search filters, rewrite rules, content replacement, and more. You can also test patterns interactively with the Regex tool in the Toolbox.

This guide covers the basics, common syntax, and practical examples to help you get started quickly.

What Is a Regular Expression

Plain text search only finds exact strings, such as reqable.com. A regular expression describes a whole class of text with rules, for example:

  • Match any email address
  • Match URLs that start with https://
  • Extract a field value from JSON
  • Batch-replace request parameters or response content

A regex consists of literal characters and special characters (metacharacters). Literals match themselves; metacharacters express more flexible matching rules.

For example:

reqable\.com

matches reqable.com in text. Here \. matches a literal dot, not “any character”.

Another example:

https?://[^\s]+

matches links that start with http:// or https://.

tip

Regex dialects differ slightly across engines (such as JavaScript, Python, and PCRE). In Reqable, regex behavior depends on each feature module. After writing a pattern, test it first with the Regex tool in the Toolbox.

Basic Syntax

Literal Characters

Letters, digits, and most symbols match themselves.

PatternMeaningExample match
abcMatches the text abcabc
123Matches the text 123123

Metacharacters

Metacharacters have special meaning and form the core of regex.

MetacharacterMeaning
.Matches any single character except a newline
^Matches the start of a string
$Matches the end of a string
*Repeats the previous element 0 or more times
+Repeats the previous element 1 or more times
?Repeats the previous element 0 or 1 time
\|OR — matches either the left or the right side
()Groups and captures the matched content
[]Character class — matches any one character inside
{}Specifies a repetition count
\Escape — removes the special meaning of a metacharacter

Escaping

To match a metacharacter literally, escape it with \.

PatternMeaning
\.Matches a literal .
\*Matches a literal *
\+Matches a literal +
\?Matches a literal ?
\( \)Matches parentheses
\\Matches a backslash \

Character Classes

Use square brackets [] to define a set of allowed characters.

PatternMeaningExample match
[abc]Matches a, b, or ca
[0-9]Matches a digit from 0 to 97
[a-z]Matches a lowercase letterm
[A-Z]Matches an uppercase letterM
[a-zA-Z0-9]Matches a letter or digitA, 9
[^0-9]Matches a non-digita, _

Predefined Character Classes

PatternMeaning
\dA digit, equivalent to [0-9]
\DA non-digit, equivalent to [^0-9]
\wA word character, equivalent to [A-Za-z0-9_]
\WA non-word character
\sA whitespace character (space, tab, newline, etc.)
\SA non-whitespace character

Quantifiers

Quantifiers specify how many times the previous element may appear.

PatternMeaning
a*a appears 0 or more times
a+a appears 1 or more times
a?a appears 0 or 1 time
a{3}a appears exactly 3 times
a{2,4}a appears 2 to 4 times
a{2,}a appears at least 2 times

By default, quantifiers are greedy and match as much as possible. Add ? after a quantifier to make it non-greedy (lazy):

PatternMeaning
.*Greedy — match as much as possible
.*?Non-greedy — match as little as possible

For the text <a>1</a><a>2</a>:

  • <a>.*</a> matches the entire <a>1</a><a>2</a>
  • <a>.*?</a> matches the shorter <a>1</a> first

Anchors

PatternMeaning
^Matches the start of a line or string
$Matches the end of a line or string
\bWord boundary
\BNon-word boundary

Examples:

  • ^https matches a string that starts with https
  • \.com$ matches a string that ends with .com
  • \bapi\b matches the whole word api, but not apikey

Groups and Captures

Parentheses () group parts of a pattern and can also capture matches for later reference or replacement.

(https?)://([^/\s]+)(/[^\s]*)?

Meaning:

  1. Group 1: protocol http or https
  2. Group 2: hostname
  3. Group 3: optional path

In replacements, you can usually refer to groups with $1, $2, $3. For example, transform:

key=12345

into:

key=api-$1

When the match pattern is key=(\d+), the result becomes:

key=api-12345

If you only need grouping and not capturing, use a non-capturing group:

(?:http|https)

Alternation

Use | for “or”:

GET|POST|PUT

This matches GET, POST, or PUT.

With grouping, it is clearer:

^(GET|POST|PUT)$

Common Flags

Some engines support flags. Common ones include:

FlagMeaning
iCase-insensitive
gGlobal — find all matches, not just the first
mMultiline — ^ and $ match the start and end of each line
sDotall — . can also match newlines

For example, with the i flag, reqable also matches Reqable and REQABLE.

Practical Examples

1. Match HTTP Methods

^(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)$

2. Match a Domain Name

^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Matches values such as reqable.com and api.example.com.

3. Match a URL

https?://[^\s"'<>]+

Matches http or https links until whitespace or a common delimiter is reached.

4. Match an IP Address (Simplified)

\b\d{1,3}(\.\d{1,3}){3}\b

Matches values such as 127.0.0.1 and 192.168.1.1. This is a simplified pattern and does not strictly validate that each octet is ≤ 255.

5. Match an Email Address

[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}

6. Match a Phone Number (Simplified US Format)

^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$

Matches values such as 4155552671, (415) 555-2671, and +1-415-555-2671.

7. Extract a Query Parameter Value

Given this URL:

https://reqable.com?key=12345&foo=bar

Extract the value of key:

[?&]key=([^&]*)

Capture group 1 is 12345.

8. Extract a JSON Field Value

Given this text:

{"token":"abc123","expired":false}

Extract the value of token:

"token"\s*:\s*"([^"]*)"

Capture group 1 is abc123.

caution

Parsing complex JSON with regex is not robust. It is fine for simple structures; for complex cases, use a JSON parser instead.

9. Match an Authorization Header

(?i)^Authorization:\s*Bearer\s+(\S+)

Case-insensitively matches Authorization: Bearer xxx and captures the token.

10. Replacement Example: Add a Prefix to a Parameter Value

Original text:

key=12345&foo=bar

Match:

key=([^&]*)

Replace with:

key=api-$1

Result:

key=api-12345&foo=bar

Writing Tips

  1. Define the goal first: Decide what should and should not match before writing the pattern.
  2. Prefer non-greedy matching: When extracting content inside tags or quotes, use .*? to avoid over-matching.
  3. Watch escaping: Dots, question marks, parentheses, slashes, and similar characters often need escaping.
  4. Anchor when possible: Use ^, $, and \b to narrow the match and reduce false positives.
  5. Test before applying: Validate the pattern with Reqable’s Regex tool in the Toolbox before using it in search or rewrite rules.
  6. Keep it simple: If contains or wildcard matching is enough, you do not need a complex regex.

Quick Reference

NeedExample pattern
Any character (except newline).
One or more digits\d+
Optional ss?
Starts with api^api
Ends with .json\.json$
Match a or ba\|b
Capture digits(\d+)
Non-greedy any content.*?
Non-whitespace token\S+

With these basics, you can cover most regex use cases in day-to-day Reqable debugging. For more complex rules, build them step by step and validate against real request/response content.