Text Extraction
WhatsApp Chat Line (Android Export) Regex
When you export a chat from WhatsApp on Android, each message line looks like 12/01/2026, 10:15 pm - Ali Test: Hello. This pattern splits such a line into five groups: the date, the time, an optional am or pm marker, the sender name, and the message text. The m flag lets it work line by line on the whole export file, and the i flag accepts both am and AM. System notices such as encryption messages have no sender colon and are skipped. Continuation lines of multi-line messages do not start with a date, so append them to the previous message when you parse the file.
Pattern (JavaScript)
/^(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2})\s?([ap]m)? - ([^:]+): (.*)$/imStarts at the start of the line/string, then a group (a digit (0-9), between 1 and 2 times, then the character "/", then a digit (0-9), between 1 and 2 times, then the character "/", then a digit (0-9), between 2 and 4 times), then the text ", ", then a group (a digit (0-9), between 1 and 2 times, then the character ":", then exactly 2 digits), then a whitespace character (space, tab, newline), optionally (zero or one time), then a group (one of the characters ap, then the character "m"), optionally (zero or one time), then the text " - ", then a group (any character except :, one or more times), then the text ": ", then a group (any character, zero or more times), then the end of the line/string.
Examples
Matches
- ✓ 12/01/2026, 10:15 pm - Ali Test: Hello
- ✓ 3/4/26, 09:05 - Sara Demo: Meeting at five
- ✓ 12/01/2026, 10:15 PM - Ali Test: See you
Does not match
- ✗ [12/01/2026, 10:15:32 PM] Ali Test: Hello
- ✗ 12/01/2026, 10:15 pm - Messages are end-to-end encrypted.
- ✗ Hello there
Try it
3 matches (JavaScript engine)
12/01/2026, 10:15 pm - Ali Test: Hello 3/4/26, 09:05 - Sara Demo: Meeting at five 12/01/2026, 10:15 PM - Ali Test: See you [12/01/2026, 10:15:32 PM] Ali Test: Hello 12/01/2026, 10:15 pm - Messages are end-to-end encrypted. Hello there
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(\d{1,2}\/\d{1,2}\/\d{2,4}), (\d{1,2}:\d{2})\s?([ap]m)? - ([^:]+): (.*)$/gim;
const text = "12/01/2026, 10:15 pm - Ali Test: Hello";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2})\s?([ap]m)? - ([^:]+): (.*)$', re.I | re.M)
text = '12/01/2026, 10:15 pm - Ali Test: Hello'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2})\s?([ap]m)? - ([^:]+): (.*)$#im';
$text = '12/01/2026, 10:15 pm - Ali Test: Hello';
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("^(\\d{1,2}/\\d{1,2}/\\d{2,4}), (\\d{1,2}:\\d{2})\\s?([ap]m)? - ([^:]+): (.*)$", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
String text = "12/01/2026, 10:15 pm - Ali Test: Hello";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?im)^(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2})\s?([ap]m)? - ([^:]+): (.*)$`)
text := "12/01/2026, 10:15 pm - Ali Test: Hello"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2})\s?([ap]m)? - ([^:]+): (.*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
var text = "12/01/2026, 10:15 pm - Ali Test: Hello";
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"(?im)^(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2})\s?([ap]m)? - ([^:]+): (.*)$").unwrap();
let text = "12/01/2026, 10:15 pm - Ali Test: Hello";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(\d{1,2}\/\d{1,2}\/\d{2,4}), (\d{1,2}:\d{2})\s?([ap]m)? - ([^:]+): (.*)$/i
text = '12/01/2026, 10:15 pm - Ali Test: Hello'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matches- Flag(s) m not available in this language and were left out.
Frequently asked questions
- Why is the date format sometimes day first and sometimes month first?
- WhatsApp uses the date format of the phone's region settings. The pattern accepts both orders, but you must know the export's locale to turn the date into a real calendar date.
- Why does my export have a strange space before pm?
- Newer exports may put a narrow no-break space between the time and am or pm. JavaScript's \s matches that character, so the optional \s? still works.
- How do I handle messages that span several lines?
- Only the first line of a message starts with the date. Read the file line by line and append any line that does not match to the previous message.