Reputation: 2048
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.
Upvotes: 21
Views: 18667
Reputation: 4602
Regular expression examples for
Decimals input
Positive Integers ^\d+$
Negative Integers ^-\d+$
Integer ^-?\d+$
Positive Number ^\d*\.?\d+$
Negative Number ^-\d*\.?\d+$
Positive Number or Negative Number ^-?\d*\.?\d+$
Phone number ^\+?[\d\s]{3,}$
Phone with code ^\+?[\d\s]+\(?[\d\s]{10,}$
Year 1900-2099 ^(19|20)\d{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}$
Alphabetic input
^[\w.']{2,}(\s[\w.']{2,})+$
^[\w\d_.]{4,}$
^.{6,}$
^.{6,}$|^$
^[_]*([a-z0-9]+(\.|_*)?)+@([a-z][a-z0-9-]+(\.|-*\.))+[a-z]{2,6}$
^([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}$
Upvotes: 38
Reputation: 15118
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.
Upvotes: 2
Reputation: 24167
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.
Upvotes: 5
Reputation: 28207
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.
Upvotes: 1
Reputation: 2534
Upvotes: 1
Reputation: 124297
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.
Upvotes: 2
Reputation: 4226
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.
Upvotes: 3
Reputation: 39485
How can they help me in my every day tasks?
A daily use for programmers could include
dir *.txt
does this)to name just a few
Upvotes: 1
Reputation: 476594
Upvotes: 3
Reputation: 91299
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.
Upvotes: 4