PatternRegex

Match text in double quotes

Pull the phrase between a pair of double quotes out of a transcript, article, or log line for quick extraction.

Last updated

Patterng
"[^"\n]*"
Open in Regex Tester & Builder

Matches

  • She said "hello there" to him
  • The sign read "Open 24 hours" by the door

Doesn’t match

  • No quotes in this sentence
  • A single " mark with no partner

Why it’s written this way

The pattern opens on a literal double quote, then [^"\n]* greedily consumes any run of characters that is neither another quote nor a newline, before a closing double quote ends the match.

Excluding \n from the middle class keeps a single stray quote from swallowing everything up to the next quote several lines down — each match stays confined to one line.

Edge cases to know

  • →It has no concept of an escaped quote, so a string containing \"hi\" as an escaped pair ends the match at that embedded quote rather than the intended closing one.
  • →Curly or ‘smart’ quotes from a word processor aren't recognized at all — only the straight ASCII double quote counts.
  • →An odd number of quote characters in the text leaves the final one unmatched rather than flagged as an error; the pattern simply stops finding pairs.

Part by part

[^"\n]*
Any run of characters that is neither a quotation mark nor a newline. Excluding the quote is what stops the match spanning from the first opening quote to the last closing one.

Common mistakes

  • ".*"

    The greedy dot runs to the last quotation mark on the line, so a sentence with two quoted phrases returns a single match covering both and the unquoted text between them.

  • ".*?"

    A lazy quantifier fixes the greediness but still allows newlines in some engines, and it is slower on long lines because it backtracks at every position. Excluding the delimiter outright is both faster and clearer.

  • ["'].*?["']

    Accepting either delimiter lets a match open on a double quote and close on an apostrophe, so an ordinary contraction terminates a quotation early. Match one delimiter type at a time.

Questions

Does it handle escaped quotes inside the string?

No. A backslash-escaped quote ends the match early, so a string containing one is truncated. Handling escapes properly needs an alternation that consumes backslash pairs, at which point a real parser is usually the better answer.

Why exclude newlines?

Because an unclosed quotation mark would otherwise let the match run to the next quote several paragraphs away. Confining it to one line makes the failure local and obvious instead of silently enormous.

Does it match smart quotes?

No. Word processors substitute curly quotation marks, which are different characters entirely, so text pasted from one will not match. Normalise them first, or add the curly pair as a separate alternative.

Can I use this to extract JSON string values?

For simple, trusted input it usually works. For anything real, use a JSON parser — escaped quotes, unicode escapes and nested structure all defeat this pattern in ways that produce plausible wrong answers.

Related in Patterns