tags:

views:

198

answers:

10

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.

+3  A: 

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.

JG
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.
Anders Eurenius
+1  A: 
  • 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,...)
  • ...
CommuSoft
+1  A: 

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

akf
+1  A: 

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.

Anders Eurenius
That's just up to the next tag. May or may not be a closing tag.
recursive
@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..
Anders Eurenius
+1  A: 

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.

chaos
+6  A: 

There a lot on this for php javascript or whatever else... For exemple is use this tooltips...

Regular expression examples for decimals input

Positive Integers --- ^\d+$
Negative Integers --- ^-\d+$
Integer --- ^-{0,1}\d+$
Positive Number --- ^\d*\.{0,1}\d+$
Negative Number --- ^-\d*\.{0,1}\d+$
Positive Number or Negative Number - ^-{0,1}\d*\.{0,1}\d+$
Phone number --- ^\+?[\d\s]{3,}$
Phone with code --- ^\+?[\d\s]+\(?[\d\s]{10,}$
Year 1900-2099 --- ^(19|20)[\d]{2,2}$
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}$

Regular expression examples for 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]|$

var reWhiteSpace = /^\s+$/;
var reDigit = /^\d$/;
var reInteger = /^\d+$/;
var reSignedInteger = /^(\+|\-)?\d+$/;
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
var reSignedFloat = /^(((\+|\-)?\d+(\.\d*)?)|((\+|\-)?(\d*\.)?\d+))$/;
var reLetter = /^[a-zA-Z]$/;
var reAlphabetic = /^[a-zA-Z]+$/;
var reLetterOrDigit = /^([a-zA-Z]|\d)$/;
var reAlphanumeric = /^[a-zA-Z0-9]+$/;
var reEmail = /^([a-z\d]+([\.\-_]?[a-z\d]+)*)@([a-z\d]+[\.\-]?[a-z\d]+|[\.]?[a-z\d]+)+\.([a-z]{2}|com|net|org|edu|biz}info|gov)$/i;
var reZipCode = /^\d{5}$/;
var reDep = /^((\d\d)|(2A)|(2B)|(97[1-6]))$/;
//var reDate = /^(\d{2}\/){2}\d{4}$/;
var reDate = /^\d{4}(\-\d{2}){2}$/;
var reUrl = /^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}$/;

http://code.google.com/p/molokoloco-coding-project/wiki/RegexSyntax *i know i had to clear that page a little bit more -but hop that help-

molokoloco
A: 
  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
Asim Sajjad
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
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.
Jahanzeb Farooq
+2  A: 

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.

pavium
+1  A: 

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.

RC
+3  A: 

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.

bobbymcr