Thought regular expressions were too tough to master? If so, think again! In this article Jan runs you through everything you need to know, no matter which programming language you use!
The Complete Regular Expression Guide - Wildcards (Page 4 of 5 )
For people who have some knowledge of wildcards, I will give you a brief explanation of how to convert them to regular expressions. A typical wildcard looks like this:
*.jpg
... and matches any text which end with .jpg. You can also specify brackets with characters, for example:
*.[ch]pp
...matches any text which ends in .cpp or .hpp.
Examples To really get to know regular expressions, I've left some commonly used expressions on this page. Study them, experiment with them and try to understand exactly what they accomplish.
Email validity will only match email addresses which are valid, for instance user@host.com:
Protocol validity matches web based protocols such as htpp://, ftp:// or https://
[a-z]+://
C/C++ includes matches valid include statements in C/C++ files:
^#include[ \t]+[<"][^>"]+[">]
C++ end of line comments
//.+$
Floating point numbers match simple floating point numbers such as 1.2 and 0.5:
-?[0-9]+\.[0-9]+
Hexadecimal numbers match C/C++ style hex numbers such as 0xcafebabe:
0x[0-9a-fA-F]+
Utilities There are several utilities that use regular expressions for data extraction, text validation, etc. I will leave you with brief descriptions of some of them.
grep Grep searches named input files for lines containing a match to the given pattern. It can also be used to find files that contain a specific pattern, for instance:
grep -E "cow|vache" * >/dev/null && echo "Found a cow"
This utility is rather common on Linux distributions, but if you don't have it you can grab a version on the GNU page.
A small tip is to enable extended regular expressions with the –E option. If you don't enable it, then a lot of the meta-characters explained earlier in this article won't work.
sed Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream. This utility is rather common on Linux distributions, but if you don't have it you can grab a version on the GNU page.
gawk Gawk is the GNU Project's implementation of the AWK programming language. It conforms to the definition of the language in the POSIX 1003.2 command Language and utilities standard. This is utility is rather common on Linux distributions, but if you don't have it you can grab a version on the GNU page.
RegExplorer RegExplorer is a visual regular expression explorer. It allows for writing regular expressions and visually seeing matches, thus making regular expression much easier to write and maintain. You can grab a copy at the RegExplorer page.
Visual RegExp This software will let you design your regexp’s by letting you type the expression and visualize its effect on a sample piece of text. You can grab a copy at the Visual RegExp page.
Regexx Regexx is a complete regular expression solution for C++. It implements easy expression execution, global searching, replace with atom substitution, customized replaces, easy match and atom strings retrieving. Grab a copy at it's home page.