How to Extract Data from a WhatsApp Chat Export
Published · 5 min read
WhatsApp lets you export a chat as a plain text file. That file is a useful source of data: who said what and when, links that were shared, phone numbers, dates and order details. The text is semi-structured, with every message starting with a timestamp and a sender name, which makes it a good fit for regex. This guide shows how to match message lines from both Android and iOS exports, deal with the quirks, and turn the result into data you can count, filter or save as a spreadsheet.
What an exported chat looks like
The exact format of the export is not fixed. It depends on the device, the operating system, the app version and the phone's language and region settings. Two styles are common, and the examples below use fictional names and messages.
Android style
12/01/2026, 10:15 pm - Ali Test: See you at 5
12/01/2026, 10:16 pm - Sara Test: Sure, bring the notes
the ones from MondayiOS style
[12/01/2026, 10:15:32 PM] Ali Test: Running late
[12/01/2026, 10:17:05 PM] Sara Test: No problemLook closely and the differences are clear. The iOS style wraps the timestamp in square brackets and includes seconds, while the Android style uses a dash between the timestamp and the sender. Beyond those two styles, expect variation: the date may be written day first or month first, the year may have two or four digits, and the time may use a 24-hour clock with no am or pm at all. Always open your own file and check a few lines before writing the pattern.
Matching one message line
Each message line has four parts worth capturing: the date, the time, the sender and the message text. The pattern describes them in order and wraps each in a capture group. The m flag makes ^ and $ work per line, which is essential because the whole chat is one long string. If you are new to groups, the groups and anchors lessons explain both ideas.
Pattern for Android-style lines
^(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2}(?:\s?[ap]m)?) - ([^:\n]+): (.*)$Use it with the m and i flags. The i flag lets [ap]m match PM as well as pm. The am or pm part is optional so that 24-hour exports also match. The sender group [^:\n]+ takes everything up to the first colon, which works because the time's colon has already been consumed by that point. Using a negated set here instead of .* keeps the match on one line. The library entry for an Android chat line has a tested version of this pattern.
Pattern for iOS-style lines
^\[(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2}:\d{2}(?:\s?[AP]M)?)\] ([^:\n]+): (.*)$The square brackets must be escaped as \[ and \] because they normally start a character set. The time includes seconds. See the iOS chat line entry for the library version.
Detecting which style you have
If your tool needs to accept exports from different phones, do not try to squeeze both styles into one complicated pattern. Check the first few lines instead: if a line starts with [ followed by a digit, use the iOS pattern, otherwise use the Android one. Two clear patterns are much easier to read, test and fix than one pattern full of optional parts.
const isIos = /^\[\d/m.test(chat);
const LINE = isIos ? IOS_LINE : ANDROID_LINE;Quirks that break simple patterns
- Invisible characters: some exports contain characters you cannot see, such as a narrow no-break space before am or pm, or a direction mark at the start of a line. Using
\srather than a literal space helps with the first. If a pattern looks right but does not match, check for hidden characters. - Multi-line messages: when someone writes a message with line breaks, only the first line starts with a timestamp. The following lines are plain text and must be joined to the previous message in code.
- System messages: notices such as a group being created or a member being added usually have no sender followed by a colon, so the pattern skips them. Review what is skipped in case one of them does contain a colon.
- Media placeholders: attachments appear as placeholder text instead of the file when you export without media. The exact wording varies by version and language.
- Locale differences: a date like
05/03/2026could mean the fifth of March or the third of May. The regex captures it as text; interpret it according to the phone's settings.
Parsing a whole chat in JavaScript
The code below reads the export line by line. When a line matches the message pattern, it starts a new message; when it does not, the line is added to the previous message's text. This handles multi-line messages cleanly.
const LINE = /^(\d{1,2}\/\d{1,2}\/\d{2,4}), (\d{1,2}:\d{2}(?:\s?[ap]m)?) - ([^:\n]+): (.*)$/i;
function parseChat(text) {
const messages = [];
for (const line of text.split(/\r?\n/)) {
const m = line.match(LINE);
if (m) {
const [, date, time, sender, body] = m;
messages.push({ date, time, sender, body });
} else if (messages.length > 0) {
messages[messages.length - 1].body += "\n" + line;
}
}
return messages;
}Because the code tests one line at a time, it does not need the m flag. Slashes are escaped as \/ here because the pattern is written as a JavaScript regex literal, which is delimited by slashes. Swap in the iOS pattern if your export uses that style.
Pulling out useful data
Once messages are separated, extracting specific values is ordinary regex work on the message text.
Messages per sender in Python
import re
from collections import Counter
LINE = re.compile(
r"^(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2}(?:\s?[ap]m)?) - ([^:\n]+): (.*)$",
re.M | re.I,
)
with open("chat.txt", encoding="utf-8") as f:
chat = f.read()
counts = Counter(m.group(3) for m in LINE.finditer(chat))
print(counts.most_common())Links, numbers and dates
To collect shared links, search the message text with https?://\S+. This simple version may include a trailing full stop or bracket, so strip common punctuation from the end of each result. For phone numbers inside messages, the approach in how to extract phone numbers applies directly, and the mention pattern helps if your export contains @mentions.
https?://\S+Filtering by date or keyword
To keep only messages from a particular day, filter on the captured date string rather than writing a new regex for each date. For a range of dates, convert the captured parts into real date values in code first, taking the day and month order from your export, and then compare those. Comparing date strings directly gives wrong answers when the day comes first.
Keyword searches work the same way. Run a small pattern such as \binvoice\b with the i flag against each message body, and you get every message that mentions the word, with its sender and timestamp already attached. That is often far more useful than searching the raw file, where a match tells you nothing about who wrote it or when.
Saving the results
A list of message objects is easy to write out as CSV, with one row per message and columns for date, time, sender and text. Remember to quote the text column, because messages often contain commas and line breaks. From there, a spreadsheet can sort, filter and chart the conversation without any further regex.
Before sharing any output, remove anything you do not need. A summary of message counts per day rarely needs names or message text, and leaving them out protects everyone in the chat.
Describe your chat format and test the generated pattern on a sample, entirely in your browser. Build a pattern for your export →
Frequently asked questions
- Why does my WhatsApp regex match nothing?
- The export format varies with device, OS, app version and locale. Compare your file's first lines with the pattern, check for brackets, seconds, 24-hour times and hidden characters, and enable the multiline flag.
- How do I handle messages that span several lines?
- Process the file line by line. Start a new message when a line matches the timestamp pattern, and append any other line to the previous message.
- Is it safe to paste a chat export into RegexLab?
- RegexLab runs entirely in your browser and does not send your text anywhere. Still treat the export as private data and avoid pasting it into online tools that upload content.
- Can I tell whether a date is day first or month first?
- Not from the regex alone. The pattern captures the date as text; check the phone's language and region settings, or look for a day value above 12 in the file.
Related posts
- How to Extract Anything from Text with RegexA repeatable regex method for pulling codes, emails, dates and amounts out of messy text, with tested patterns and JavaScript and Python code.
- How to Extract Phone Numbers with RegexRegex patterns to find phone numbers in text: local formats, international E.164 numbers and a loose extractor, plus cleanup in JS and Python.
- How to Clean Messy Data with RegexPractical regex find-and-replace recipes for messy data: whitespace, duplicate words and lines, dates, phone numbers, leftover HTML and CSV fields.