Text Extraction
Text Between Single Quotes Regex
Single quotes wrap string literals in SQL, shell scripts, YAML files and many programming languages. This pattern finds an opening apostrophe, collects every character that is not another apostrophe, and stops at the closing one. Group 1 contains the literal value. It is handy when you want to list the values in a WHERE clause, pull arguments out of a shell command, or audit hard-coded strings in a script. Keep in mind that ordinary English contractions also use the apostrophe, so free text with words like it's or don't can produce surprising pairs when two contractions appear on the same line.
Pattern (JavaScript)
/'([^']*)'/Matches the character "'", then a group (any character except ', zero or more times), then the character "'".
Examples
Matches
- ✓ WHERE name = 'Ali Test'
- ✓ echo 'hello world'
- ✓ mode: 'dark'
Does not match
- ✗ no quotes here
- ✗ don't stop
- ✗ "double quoted"
Try it
3 matches (JavaScript engine)
WHERE name = 'Ali Test' echo 'hello world' mode: 'dark' no quotes here don't stop "double 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 = "WHERE name = 'Ali Test'";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r"'([^']*)'")
text = 'WHERE name = \'Ali Test\''
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\'([^\']*)\'/';
$text = 'WHERE name = \'Ali Test\'';
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 = "WHERE name = 'Ali Test'";
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 := "WHERE name = 'Ali Test'"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"'([^']*)'");
var text = "WHERE name = 'Ali Test'";
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 = "WHERE name = 'Ali Test'";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /'([^']*)'/
text = 'WHERE name = \'Ali Test\''
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I handle SQL strings that contain a doubled quote like 'it''s'?
- SQL escapes an apostrophe by writing it twice. Use '((?:[^']|'')*)' so that a pair of quotes is consumed as part of the value instead of ending it.
- Why does this match across two contractions in normal prose?
- In a sentence such as "don't worry, it's fine" the first apostrophe opens a match and the second one closes it. Single-quote extraction works best on code and data, not on free-form English text.
- Does group 1 include the quote marks?
- No. The quotes sit outside the capture group, so group 1 holds only the inner text. Use group 0, the whole match, if you need the quotes as well.