I don't know about you, but I find regluar expressions quite difficult. Apart from a very simple, I almost always have to look it up or use a reference book.

Yesterday, one of my colleagues needed a regex for filtering on Google Analytics. The value needed to be greater than 1200 to be valid.

In case you are looking for a regex for something similar, I though I would post it so you can alter it for your needs.

Regex greater than 1200

^[1-9][2-9](?!00$)[0-9][1-9]?\d+$

I used this tool to test it.

And this is the tool's English interpretation of what the regex is doing:

^[1-9][2-9](?!00$)[0-9][1-9]?\d+$/
  • ^ assert position at start of the string
  • [1-9] match a single character present in the list below
    • 1-9 a single character in the range between 1 and 9
  • [2-9] match a single character present in the list below
    • 2-9 a single character in the range between 2 and 9
  • (?!00$) Negative Lookahead - Assert that it is impossible to match the regex below
    • 00 matches the characters 00 literally
    • $ assert position at end of the string
  • [0-9] match a single character present in the list below
    • 0-9 a single character in the range between 0 and 9
  • [1-9]? match a single character present in the list below
    • Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
    • 1-9 a single character in the range between 1 and 9
  • \d+ match a digit [0-9]
    • Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
  • $ assert position at end of the string

Summary

The key part to this is the look aheadĀ (?!00$), this makes sure that it won't end with 00 after the first 2 digits. Without this in there, it would say 1200 is valid, which violates the rule of greater than

Anyway, I hope someone finds this useful one day. Or me 6 months time when I've forgotten and need something similar.

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
stkrs

  • Custom stickers for startups, artists and brands.
  • Bespoke easy-apply, removable, custom brand stickers printed in the UK.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.