Text Extraction
WhatsApp Chat Line (iPhone Export) Regex
Chat exports from WhatsApp on iPhone use a different layout from Android. The date and a time with seconds are wrapped in square brackets, as in [12/01/2026, 10:15:32 PM] Ali Test: Hello, and there is no dash before the sender. This pattern captures the date, the time, the optional AM or PM marker, the sender and the message text in five groups. The m flag processes a whole export line by line and the i flag accepts any letter case. Lines without the bracketed timestamp, such as continuation lines of long messages, do not match and can be joined to the previous message.
Pattern (JavaScript)
/^\[(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2}:\d{2})\s?([ap]m)?\] ([^:]+): (.*)$/imStarts at the start of the line/string, then the character "[", 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 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:32 PM] Ali Test: Hello
- ✓ [3/4/26, 09:05:10] Sara Demo: Ok
- ✓ [1/1/2026, 8:00:00 am] Ali Test: Good morning
Does not match
- ✗ 12/01/2026, 10:15 pm - Ali Test: Hello
- ✗ [12/01/2026, 10:15 PM] Ali Test: Hi
- ✗ Ali Test: Hello
Try it
3 matches (JavaScript engine)
[12/01/2026, 10:15:32 PM] Ali Test: Hello [3/4/26, 09:05:10] Sara Demo: Ok [1/1/2026, 8:00:00 am] Ali Test: Good morning 12/01/2026, 10:15 pm - Ali Test: Hello [12/01/2026, 10:15 PM] Ali Test: Hi Ali Test: Hello
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}:\d{2})\s?([ap]m)?\] ([^:]+): (.*)$/gim;
const text = "[12/01/2026, 10:15:32 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}:\d{2})\s?([ap]m)?\] ([^:]+): (.*)$', re.I | re.M)
text = '[12/01/2026, 10:15:32 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}:\d{2})\s?([ap]m)?\] ([^:]+): (.*)$#im';
$text = '[12/01/2026, 10:15:32 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}:\\d{2})\\s?([ap]m)?\\] ([^:]+): (.*)$", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
String text = "[12/01/2026, 10:15:32 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}:\d{2})\s?([ap]m)?\] ([^:]+): (.*)$`)
text := "[12/01/2026, 10:15:32 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}:\d{2})\s?([ap]m)?\] ([^:]+): (.*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
var text = "[12/01/2026, 10:15:32 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}:\d{2})\s?([ap]m)?\] ([^:]+): (.*)$").unwrap();
let text = "[12/01/2026, 10:15:32 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}:\d{2})\s?([ap]m)?\] ([^:]+): (.*)$/i
text = '[12/01/2026, 10:15:32 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 does the first line of my iPhone export not match?
- Some iPhone exports start lines with an invisible left-to-right mark, U+200E, before the bracket. Add \u200E? right after the caret, or strip that character before parsing.
- Can one pattern handle both Android and iPhone exports?
- It is possible with alternation, but the two layouts differ in brackets, seconds and the dash. Two clear patterns are easier to maintain than one complex one.
- Are attachments captured?
- Attachment lines such as image omitted still follow the same layout, so they match with the placeholder text as the message. The media itself is a separate file in the export.