Text Extraction
Key=Value Pairs Regex
Many formats store data as key=value pairs: URL query strings, cookie headers, simple config files and structured log lines. This pattern captures a word-character key in group 1, requires an equals sign immediately after it, and captures the value in group 2 up to the next space, ampersand or semicolon. Those three stop characters cover the most common separators, so a global search over a=1&b=2 or level=info user=ali returns each pair separately. Quoted values containing spaces are not handled, so for those you would add an alternative branch for a quoted string before the plain value.
Pattern (JavaScript)
/\b(\w+)=([^\s&;]+)/Matches a word boundary, then a group (one or more word characters), then the character "=", then a group (any character except \s&;, one or more times).
Examples
Matches
- ✓ name=Ali
- ✓ a=1&b=2
- ✓ mode=dark; size=12
Does not match
- ✗ no pairs here
- ✗ =value
- ✗ key = value
Try it
5 matches (JavaScript engine)
name=Ali a=1&b=2 mode=dark; size=12 no pairs here =value key = value
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\b(\w+)=([^\s&;]+)/g;
const text = "name=Ali";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\b(\w+)=([^\s&;]+)')
text = 'name=Ali'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\b(\w+)=([^\s&;]+)/';
$text = 'name=Ali';
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("\\b(\\w+)=([^\\s&;]+)");
String text = "name=Ali";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\b(\w+)=([^\s&;]+)`)
text := "name=Ali"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\b(\w+)=([^\s&;]+)");
var text = "name=Ali";
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"\b(\w+)=([^\s&;]+)").unwrap();
let text = "name=Ali";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\b(\w+)=([^\s&;]+)/
text = 'name=Ali'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I turn the matches into an object?
- Run matchAll with the g flag and set obj[m[1]] = m[2] for each match. Remember to decode URL-encoded values with decodeURIComponent when parsing query strings.
- Why does "key = value" with spaces not match?
- The pattern expects the equals sign directly after the key. Use (\w+)\s*=\s*([^\s&;]+) if your format allows spaces around it.
- Can values contain dashes or dots?
- Yes. The value part accepts any character except whitespace, ampersand and semicolon. Values such as 1.5 or en-US are captured in full.