Regex quirks: Why version formats like "1.2" trip up your validation

When using regex to enforce a consistent version number format, unexpected results can be frustrating.

3 min readMicrosoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community

There's a moment every developer knows well: the regex looks right, the online editors confirm it, and yet the system stubbornly rejects perfectly valid input. That's exactly where this user finds themselves, and the frustration is entirely justified. But here's the thing: the regex isn't the problem. The environment is.

The pattern `^(\d+\.)+\d+$` is a textbook example of a version-matching expression. It requires at least one digit, a period, and then more digits, repeated, ending with a final digit group. That should accept `1.2` and `2.7` just as easily as `1.0`. The fact that it doesn't points to something else entirely, likely a hidden character, a whitespace issue, or a line-ending quirk in the user's specific data source. Online regex editors and AI tools test against clean, isolated strings. They don't account for invisible carriage returns, non-breaking spaces, or the subtle differences between how various systems encode periods and digits. When the user says they've tried replacing `\d` with `[0-9]` and removing anchors, and nothing changed, that's a strong signal the regex engine is never even seeing the string they think it is.

This is a teachable moment, and it's one worth taking seriously. If you're building validation for user input, the regex is only half the battle. The other half is understanding what your data actually contains before you try to match it. The user's instinct to enforce a consistent version format is sound. But the real lesson is that validation isn't just about pattern matching; it's about debugging the input pipeline. Print the raw bytes. Check for trailing newlines. Compare the length of the string to what you expect. A simple `inspect` or a hex dump would have revealed the culprit in minutes, saving the hours of confusion this person has already spent.

So what's the practical takeaway? Don't abandon the regex, but don't trust it blindly either. Build a quick test that logs the exact input before validation, and use that to confirm your assumptions. The tool isn't lying to you, and neither is the AI. The problem is almost certainly in the data, not the pattern. And if you're the one writing the validation rules, remember that your job isn't just to write the rule, it's to make sure the rule sees what you think it sees. That's the difference between a form that works and one that leaves users scratching their heads over why `1.2` doesn't look like `1.0`.

From Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community

So I'm using a regex for version numbers since I want everyone to use the same format and not one person writing "Version 1" and another "1.0". My regex for this is "^(\d+\.)+\d+$" and it accepts version numbers like "1.0" or "2.0" or "1.0.3.5" but for some reason gives an error message when there's no 0 after the first period like "1.2" or "2.7". Except when the first number is a zero like "0.8".

Read the original at Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community