Pattern library / Extract & Replace
Regex to Match Quoted Strings
Config files, CSV-ish logs and template code all wrap values in quotes. The trap: ".*" is greedy and happily runs from the first opening quote to the last quote on the line. The fix is a negated class that simply refuses to cross a quote of the same kind.
How the pattern works
[^\"]*— any character except a double quote, zero or more: the match cannot cross its own closing quote.- The alternation
|'[^']*'handles both quote styles independently. - RE2 note: this version is backreference-free and GA4/BigQuery-safe. The clever
(['\"])(.*?)\1with a backreference works in JS/Python but is rejected by RE2 — see the RE2 vs PCRE guide. - Escaped quotes (
"she said \"hi\"") need(\\.|[^\"])*— the allowance for a backslash-escaped character. That variant stays RE2-compatible too.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
name="Ada Lovelace" | Match | captures "Ada Lovelace" | Try → |
key='value with spaces' | Match | single-quoted | Try → |
say "hi" then "bye" | Match | first pair only (not greedy across) | Try → |
unquoted value | No match | no quotes present | Try → |
open "but never closed | No match | no closing quote | Try → |
"mixed 'inner' quotes" | Match | inner opposite quote fine | Try → |
Where to use it
JavaScript
const re = /"[^"]*"|'[^']*'/g; 'key="hello world"'.match(re); // ["hello world"]
Python (allow escaped quotes)
import re re.findall(r'"(\\.|[^"\\])*"', 'msg="she said \\"hi\\""')
BigQuery / GA4 (RE2)
REGEXP_EXTRACT(line, r'"([^"]*)"') # group 1 = inner text
Common mistakes
- Greedy
".*"spans the whole line — the classic bug this page exists to fix. - Backreferences (
\1) break RE2: GA4, BigQuery, Go and Rust will reject the pattern outright. - Nested same-style quotes are unrepresentable in regex context-free grammars — parse with a real parser for those.
FAQ
How do I capture the text inside the quotes?
Wrap the inner class in a group: "([^"]*)" — group 1 is the content without the quote characters.
Why does ".*" match too much?
It is greedy: the . also matches quotes, so the engine takes the last quote on the line as the closer. [^"]* cannot do that by construction.
Does this handle escaped quotes?
The simple version no; use "(\\.|[^"\\])*" which lets a backslash-escaped character through while still stopping at an unescaped closing quote.
Related patterns
Extract Numbers from String — Regex + Examples · Log Level Regex — Match INFO / WARN / ERROR Lines · Remove Query Parameters with Regex (Replace Example)
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.