Text Extraction
Text Between Double Quotes Regex
Pulling quoted phrases out of a sentence, a log line or a CSV cell is one of the most common extraction jobs. This pattern looks for an opening double quote, then takes every character that is not another double quote, and finally requires the closing quote. Because the middle part refuses quote characters, the match stops at the first closing quote instead of running greedily to the last one on the line. Capture group 1 gives you the inner text, so you can collect every quoted phrase with a global search and keep only the words people actually wrote between the marks.
Pattern (JavaScript)
/"([^"]*)"/Matches the character """, then a group (any character except ", zero or more times), then the character """.
Examples
Matches
- ✓ He said "hello" and left
- ✓ "Ali Test"
- ✓ title="Quarterly report" draft
Does not match
- ✗ no quotes at all
- ✗ only one " mark
- ✗ 'single quoted'
Try it
3 matches (JavaScript engine)
He said "hello" and left "Ali Test" title="Quarterly report" draft no quotes at all only one " mark 'single quoted'
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /"([^"]*)"/g;
const text = "He said \"hello\" and left";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'"([^"]*)"')
text = 'He said "hello" and left'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/"([^"]*)"/';
$text = 'He said "hello" and left';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\"([^\"]*)\"");
String text = "He said \"hello\" and left";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`"([^"]*)"`)
text := "He said \"hello\" and left"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"""([^""]*)""");
var text = "He said \"hello\" and left";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r#""([^"]*)""#).unwrap();
let text = "He said \"hello\" and left";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /"([^"]*)"/
text = 'He said "hello" and left'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does this pattern handle escaped quotes like \" inside the string?
- No. The middle part stops at any double quote, so an escaped quote ends the match early. If your input uses backslash escapes, use a pattern such as "((?:[^"\\]|\\.)*)" that skips over a backslash and the character after it.
- Why not use ".*" to match quoted text?
- The dot star is greedy, so on a line with two quoted phrases it runs from the first opening quote to the very last closing quote. The negated class [^"]* cannot cross a quote mark, so each phrase is matched separately.
- Can the match span several lines?
- Yes. A negated character class also matches newline characters, so a quoted block that wraps across lines is still captured. If you want single-line quotes only, change the class to [^"\n]*.