20

I am new to regular expressions and have just started learning some. I was wondering what are some of the most commonly used regular expressions by the programmers. Put it in another way, I would like to know what are the regular expressions most useful for? How can they help me in my every day tasks? I would prefer to know regular expressions useful for every day programming, not occasionally used regular expressions such email address matching.

Anyone? Thanks.

Edit: Most of the answers include regular expressions to match email addresses, URLs, dates, phone numbers etc. Please note that not all programmers have to worry about these things in their every day tasks. I would like to know some more generic uses of regular expressions, if there are any, which programmers in general (may) use regardless what language are domain they are working in.

5
  • If you use anything the same way every day, you should wrap it up in a [library|class|abstraction|...]. Sep 19, 2009 at 23:51
  • Wow SO is great, I got 5 answers within 10 minutes of posting my questions. That's AWESOME! Sep 19, 2009 at 23:58
  • @dmckee: Are you sure? Isn't it easier to simply type what you mean, instead trying to build and remember the specificts of some kind of abstraction that only you use? Sep 20, 2009 at 0:01
  • 1
    @Anders Eurenius: If there is non-trivial stuff to remember you're not using it the same way. Sep 20, 2009 at 0:05
  • I use regex for validation (I just wrote one to get lat-lon pairs from user input). I also use it to extract valuable crap from unimportant crap (like pulling out and counting specific error messages from a log file)
    – RufusVS
    Feb 12, 2018 at 19:36

10 Answers 10

36

Regular expression examples for

Decimals input

  1. Positive Integers ^\d+$

  2. Negative Integers ^-\d+$

  3. Integer ^-?\d+$

  4. Positive Number ^\d*\.?\d+$

  5. Negative Number ^-\d*\.?\d+$

  6. Positive Number or Negative Number ^-?\d*\.?\d+$

  7. Phone number ^\+?[\d\s]{3,}$

  8. Phone with code ^\+?[\d\s]+\(?[\d\s]{10,}$

  9. Year 1900-2099 ^(19|20)\d{2}$

  10. Date (dd mm yyyy, d/m/yyyy, etc.)

    ^([1-9]|0[1-9]|[12][0-9]|3[01])\D([1-9]|0[1-9]|1[012])\D(19[0-9][0-9]|20[0-9][0-9])$
    

IP v4:

    ^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]){3}$

Alphabetic input

  • Personal Name ^[\w.']{2,}(\s[\w.']{2,})+$
  • Username ^[\w\d_.]{4,}$
  • Password at least 6 symbols ^.{6,}$
  • Password or empty input ^.{6,}$|^$
  • email ^[_]*([a-z0-9]+(\.|_*)?)+@([a-z][a-z0-9-]+(\.|-*\.))+[a-z]{2,6}$
  • domain ^([a-z][a-z0-9-]+(\.|-*\.))+[a-z]{2,6}$

Other regular expressions - Match no input ^$ - Match blank input ^\s\t*$ - Match New line [\r\n]|$ - Match white Space ^\s+$ - Match Url = ^http\:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$

0
5

I would take a different angle on this and say that it's most helpful to know when to use regular expressions and when NOT to use them.

For example, imagine this problem: "Figure out if a string ends with a whitespace character." A regular expression could be used here, but if you're using C#, this code is much faster:

bool EndsWithWhitespace(string s)
{
    return !string.IsNullOrEmpty(s) && char.IsWhiteSpace(s[s.Length - 1]);
}

Regular expressions are powerful, and it's important to know when they're too powerful for the problem you're trying to solve.

4

Think about input fields that require validation, such as zip codes, telephone numbers, et cetera. Regular expressions are very utilized to validate those. Also, take a look at this site, which contains many tutorials, and many more examples, some of which I present next:

Numeric Ranges. Since regular expressions work with text rather than numbers, matching specific numeric ranges requires a bit of extra care.

Matching a Floating Point Number. Also illustrates the common mistake of making everything in a regular expression optional.

Matching an Email Address. There's a lot of controversy about what is a proper regex to match email addresses. It's a perfect example showing that you need to know exactly what you're trying to match (and what not), and that there's always a trade-off between regex complexity and accuracy.

Matching Valid Dates. A regular expression that matches 31-12-1999 but not 31-13-1999.

Finding or Verifying Credit Card Numbers. Validate credit card numbers entered on your order form. Find credit card numbers in documents for a security audit.

And many, many, many more possible applications.

1
  • I have a routine somewhere for generating regexes for {0,1}-upto-n, for any n, somewhere. I just don't use it that often, since it's usually more convenient to check the number once you've read it. Sep 19, 2009 at 23:56
3
  • E-mail address
  • Website
  • File-Paths
  • Phone-numbers/Fax/ZIP and other numbers used in business (chemistry numbers, ect.)
  • file content (check if the file can be a valid XML-file,...)
  • code modification and formatting (with replacement)
  • data types (GUID, parsing of integers,...)
  • ...
3

Upto closing tag

([^<]*)

Seriously. I use combinations of that way too often for comfort... We should all ditch regex:en for peg-parsers, especially since there's a nice regex-like grammar style for them.

2
  • That's just up to the next tag. May or may not be a closing tag.
    – recursive
    Sep 20, 2009 at 1:03
  • @recursive: Sure. But where I use it, the point is to eat the contents of the field. If I wanted a correct xml parser, I'd use one.. Sep 20, 2009 at 10:17
2

Well... I kind of think your question is wrong. It sounds like you're asking about regular expressions that could/should be as much a part of one's coding, or nearly so, as things like mathematical operators. Really, if your code depends that pervasively on regular expressions, you're probably doing something very wrong. For pervasive use throughout code, you want to use data structures that are better defined and more efficient to work with than regular-expression-managed strings.

The closest thing to what you're asking for that would make much sense to me would be something like /\s+/ used for splitting strings on arbitrary amounts of whitespace.

2

This is a little like asking 'what are the most useful words for programmers?'

It depends what you're going to use them for, and it depends which language. And you didn't say.

Some programmers never need to worry about matching email addresses, phone numbers, ZIP codes and IP addresses.

My copy of

Mastering Regular Expressions, O'Reilly, 3rd Edition, 2006

devotes a lot of space to the flavours of regex used by different languages.

It's a great reference, but I found the 2nd edition more readable.

1

How can they help me in my every day tasks?

A daily use for programmers could include

  • search/replace of sample data for testing purposes
  • searching through log files for String patterns (Exceptions, for example)
  • searching a directory structure for files of a certain type (as simple as dir *.txt does this)

to name just a few

1
  1. E-mail
  2. Website URL
  3. Phone-numbers
  4. ZIP Code
  5. Alpha Numeric, (user name consist of alpha number and only start with alpha character
  6. IP Address
2
  • These examples demonstrate the point in my answer. While very useful in some areas, they would be irrelevant for me. Not all programmers need to worry about email, phone numbers, IP addresses and ZIP codes.
    – pavium
    Sep 20, 2009 at 0:02
  • Exactly this was the point of my question. I only very rarely need to use regular expressions mentioned in this answers. I was expecting answers with more generic regular expressions such as searching for comments, searching for strings with specific set of words, etc, which programmers use no matter what programming language or domain they are working in. Sep 20, 2009 at 0:16
1

This will be completely dependent on what domain you work in. For some it will be phone numbers and SSN's and others it will be email addresses, IP addresses, URLs. The most important thing is knowing when you need a regex and when you don't. For example, if you're trying to parse data from an XML or HTML file, it's usually better to use a library specifically designed to parse that content than to try and write something yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.