Skip to content

How to Extract OTP Codes From SMS Text Reliably

2026-08-28 · SMS-GO Research Team · 6 min read

Written by the team that operates SMS-GO. Figures reflect first-hand operation and an August 2026 audit of 16 providers\u2019 published price data. Pricing moves, so treat specific numbers as a snapshot.

For automation, receiving the SMS is only half the job. The code is embedded in free-form text that varies by platform and language, and a naive parser silently grabs the wrong digits. Here are the patterns that hold up in practice.

The shape of a verification message

Almost every verification SMS is prose wrapping one short numeric group: "Your verification code is 123456", "123456 is your WhatsApp code", "Use 123 456 to sign in". The code is 4 to 8 digits, and it is the only standalone numeric group of that length in the message.

A pattern that works for most platforms

Match a standalone run of 4 to 8 digits, ignoring surrounding whitespace or a mid-code space. Anchoring on word boundaries prevents grabbing digits that are part of a longer string.

  • Match 4-8 digit groups: most platforms use 6.
  • Allow an optional space inside (some send "123 456").
  • Anchor to word boundaries so phone fragments do not match.
  • If several groups match, prefer the one closest to a keyword like "code".

The edge cases that break naive parsers

Real messages trip up simple regexes.

  • Spaces inside the code: "Your code is 123 456" — strip spaces before validating length.
  • Multiple numbers: a message can contain a phone number too; pick the group near "code".
  • Localized text: non-English messages still contain the same digit pattern.
  • Codes that start with 0: never cast to a number and back, or the leading zero dies.
  • Alphanumeric codes: some platforms use letters+digits; detect the expected format per service.

Know the length per service

Most services use 6 digits, but some use 4, 5, 7, or 8. If you know the service, constrain the matcher to its length. That single constraint eliminates most wrong-digit grabs.

Validate before you use

After extraction, sanity-check: the group is the expected length, it is not all one repeated digit, and it appeared near a code-related keyword. Only then hand it to your pipeline.

Questions, answered

What regex extracts most verification codes?+

Match a standalone 4-8 digit group on word boundaries, allowing an optional internal space, and prefer the group nearest a keyword like "code".

Why does my parser grab the wrong number?+

Usually because the message contains another digit group (a phone number or date) and the pattern is not anchored to the code keyword.

How do I handle codes with a space, like "123 456"?+

Strip whitespace before checking length. The code is still six digits.

Should I convert the code to a number?+

No. Keep it as a string so leading zeros survive.

Keep reading

Need a number now?

Rent a real US number up to 30 days. If no SMS arrives within 20 minutes, the charge refunds itself.

Open account