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.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

Buy me a coffee

About the author

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.

Related Posts

How to show UTC time relative to the user's local time on a .NET website

This post gives you the code I found to help you get a user's country and culture code and display a…

Read Post

What is TDD? How to get started with Test Driven Development

This post gives you an introduction into Test Driven Development. It uses a simple, real world examp…

Read Post

How to migrate or downgrade a SQL Server database to a lower version

This post shows you how you can migrate or downgrade a SQL Server database from one version to a low…

Read Post

How to convert SQL Server Compact Edition .sdf to Full SQL and speed up Umbraco

This post shows you how to convert a SQL Compact Edition database to full SQL and speed up Umbraco

Read Post