Regular Expressions Patterns

"A Blog on Regular Expressions Patterns"

Wednesday, March 3, 2010

Wildcards

Calling these "wildcards" may actually conflict with the theoretical grammar and syntax of Perl, but in fact is the most intuitive way to think of it, and will not lead to any coding mistakes.

. Match any character
\w Match "word" character (alphanumeric plus "_")
\W Match non-word character
\s Match whitespace character
\S Match non-whitespace character
\d Match digit character
\D Match non-digit character
\t Match tab
\n Match newline
\r Match return
\f Match formfeed
\a Match alarm (bell, beep, etc)
\e Match escape
\021 Match octal char ( in this case 21 octal)
\xf0 Match hex char ( in this case f0 hexidecimal)

You can follow any character, wildcard, or series of characters and/or wildcard with a repetiton. Here's where you start getting some power:

* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times

Now for some examples:

$string =~ m/\s*rem/i; #true if the first printable text is rem or REM

$string =~ m/^\S{1,8}\.\S{0,3}/; # check for DOS 8.3 filename
# (note a few illegals can sneak thru)

Matching Characters

Most letters and characters will simply match themselves. For example, the regular expression test will match the string "test" exactly. (You can enable a case-insensitive mode that would let this RE match "Test" or "TEST" as well; more about this later.)

There are exceptions to this rule; some characters are special, and don't match themselves. Instead, they signal that some out-of-the-ordinary thing should be matched, or they affect other portions of the RE by repeating them. Much of this document is devoted to discussing various metacharacters and what they do.

Here's a complete list of the metacharacters; their meanings will be discussed in the rest of this HOWTO.

. ^ $ * + ? { [ ] \ | ( )

The first metacharacters we'll look at are "[" and "]". They're used for specifying a character class, which is a set of characters that you wish to match. Characters can be listed individually, or a range of characters can be indicated by giving two characters and separating them by a "-". For example, [abc] will match any of the characters "a", "b", or "c"; this is the same as [a-c], which uses a range to express the same set of characters. If you wanted to match only lowercase letters, your RE would be [a-z].

Metacharacters are not active inside classes. For example, [akm$] will match any of the characters "a", "k", "m", or "$"; "$" is usually a metacharacter, but inside a character class it's stripped of its special nature.

You can match the characters not within a range by complementing the set. This is indicated by including a "^" as the first character of the class; "^" elsewhere will simply match the "^" character. For example, [^5] will match any character except "5".

Perhaps the most important metacharacter is the backslash, "\". As in Python string literals, the backslash can be followed by various characters to signal various special sequences. It's also used to escape all the metacharacters so you can still match them in patterns; for example, if you need to match a "[" or "\", you can precede them with a backslash to remove their special meaning: \[ or \\.

Some of the special sequences beginning with "\" represent predefined sets of characters that are often useful, such as the set of digits, the set of letters, or the set of anything that isn't whitespace. The following predefined special sequences are available:

\d
Matches any decimal digit; this is equivalent to the class [0-9].

\D
Matches any non-digit character; this is equivalent to the class [^0-9].

\s
Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].

\S
Matches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v].

\w
Matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].

\W
Matches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_].

These sequences can be included inside a character class. For example, [\s,.] is a character class that will match any whitespace character, or "," or ".".

The final metacharacter in this section is .. It matches anything except a newline character, and there's an alternate mode (re.DOTALL) where it will match even a newline. "." is often used where you want to match ``any character''.

Python Regular Expression Syntax

A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).

Regular expressions can be concatenated to form new regular expressions; if A and B are both regular expressions, then AB is also a regular expression. In general, if a string p matches A and another string q matches B, the string pq will match AB. This holds unless A or B contain low precedence operations; boundary conditions between A and B; or have numbered group references. Thus, complex expressions can easily be constructed from simpler primitive expressions like the ones described here. For details of the theory and implementation of regular expressions, consult the Friedl book referenced above, or almost any textbook about compiler construction.

A brief explanation of the format of regular expressions follows. For further information and a gentler presentation, consult the Regular Expression HOWTO, accessible from http://www.python.org/doc/howto/.

Regular expressions can contain both special and ordinary characters. Most ordinary characters, like "A", "a", or "0", are the simplest regular expressions; they simply match themselves. You can concatenate ordinary characters, so last matches the string 'last'. (In the rest of this section, we'll write RE's in this special style, usually without quotes, and strings to be matched 'in single quotes'.)

Some characters, like "|" or "(", are special. Special characters either stand for classes of ordinary characters, or affect how the regular expressions around them are interpreted.

The special characters are:

"."
(Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline.

"^"
(Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.

"$"
Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline. foo matches both 'foo' and 'foobar', while the regular expression foo$ matches only 'foo'. More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches 'foo2' normally, but 'foo1' in MULTILINE mode.

"*"
Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions as are possible. ab* will match 'a', 'ab', or 'a' followed by any number of 'b's.

"+"
Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match 'a' followed by any non-zero number of 'b's; it will not match just 'a'.

"?"
Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either 'a' or 'ab'.

*?, +?, ??
The "*", "+", and "?" qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn't desired; if the RE <.*> is matched against '

title

'
, it will match the entire string, and not just '

'. Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using .*? in the previous expression will match only '

'.

{m}
Specifies that exactly m copies of the previous RE should be matched; fewer matches cause the entire RE not to match. For example, a{6} will match exactly six "a" characters, but not five.

{m,n}
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5} will match from 3 to 5 "a" characters. Omitting m specifies a lower bound of zero, and omitting n specifies an infinite upper bound. As an example, a{4,}b will match aaaab or a thousand "a" characters followed by a b, but not aaab. The comma may not be omitted or the modifier would be confused with the previously described form.

{m,n}?
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible. This is the non-greedy version of the previous qualifier. For example, on the 6-character string 'aaaaaa', a{3,5} will match 5 "a" characters, while a{3,5}? will only match 3 characters.

"\"
Either escapes special characters (permitting you to match characters like "*", "?", and so forth), or signals a special sequence; special sequences are discussed below.

If you're not using a raw string to express the pattern, remember that Python also uses the backslash as an escape sequence in string literals; if the escape sequence isn't recognized by Python's parser, the backslash and subsequent character are included in the resulting string. However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so it's highly recommended that you use raw strings for all but the simplest expressions.

[]
Used to indicate a set of characters. Characters can be listed individually, or a range of characters can be indicated by giving two characters and separating them by a "-". Special characters are not active inside sets. For example, [akm$] will match any of the characters "a", "k", "m", or "$"; [a-z] will match any lowercase letter, and [a-zA-Z0-9] matches any letter or digit. Character classes such as \w or \S (defined below) are also acceptable inside a range. If you want to include a "]" or a "-" inside a set, precede it with a backslash, or place it as the first character. The pattern []] will match ']', for example.

You can match the characters not within a range by complementing the set. This is indicated by including a "^" as the first character of the set; "^" elsewhere will simply match the "^" character. For example, [^5] will match any character except "5", and [^^] will match any character except "^".

"|"
A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the "|" in this way. This can be used inside groups (see below) as well. As the target string is scanned, REs separated by "|" are tried from left to right. When one pattern completely matches, that branch is accepted. This means that once A matches, B will not be tested further, even if it would produce a longer overall match. In other words, the "|" operator is never greedy. To match a literal "|", use \|, or enclose it inside a character class, as in [|].

(...)
Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals "(" or ")", use \( or \), or enclose them inside a character class: [(] [)].

(?...)
This is an extension notation (a "?" following a "(" is not meaningful otherwise). The first character after the "?" determines what the meaning and further syntax of the construct is. Extensions usually do not create a new group; (?P<name>...) is the only exception to this rule. Following are the currently supported extensions.

(?iLmsux)
(One or more letters from the set "i", "L", "m", "s", "u", "x".) The group matches the empty string; the letters set the corresponding flags (re.I, re.L, re.M, re.S, re.U, re.X) for the entire regular expression. This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the compile() function.

Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.

(?:...)
A non-grouping version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.

(?P<name>...)
Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named. So the group named 'id' in the example above can also be referenced as the numbered group 1.

For example, if the pattern is (?P[a-zA-Z_]\w*), the group can be referenced by its name in arguments to methods of match objects, such as m.group('id') or m.end('id'), and also by name in pattern text (for example, (?P=id)) and replacement text (such as \g).

(?P=name)
Matches whatever text was matched by the earlier group named name.

(?#...)
A comment; the contents of the parentheses are simply ignored.

(?=...)
Matches if ... matches next, but doesn't consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it's followed by 'Asimov'.

(?!...)
Matches if ... doesn't match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it's not followed by 'Asimov'.

(?<=...)
Matches if the current position in the string is preceded by a match for ... that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in "abcdef", since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning that abc or a|b are allowed, but a* and a{3,4} are not. Note that patterns which start with positive lookbehind assertions will never match at the beginning of the string being searched; you will most likely want to use the search() function rather than the match() function:

>>> import re >>> m = re.search('(?<=abc)def', 'abcdef') >>> m.group(0) 'def' 

This example looks for a word following a hyphen:

>>> m = re.search('(?<=-)\w+', 'spam-egg') >>> m.group(0) 'egg' 

(?
Matches if the current position in the string is not preceded by a match for .... This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length. Patterns which start with negative lookbehind assertions may match at the beginning of the string being searched.

(?(id/name)yes-pattern|no-pattern)
Will try to match with yes-pattern if the group with given id or name exists, and with no-pattern if it doesn't. |no-pattern is optional and can be omitted. For example, (<)?(\w+@\w+(?:\.\w+)+)(?(1)>) is a poor email matching pattern, which will match with '' as well as 'user@host.com', but not with '. New in version 2.4.

The special sequences consist of "\" and a character from the list below. If the ordinary character is not on the list, then the resulting RE will match the second character. For example, \$ matches the character "$".

\number
Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) \1 matches 'the the' or '55 55', but not 'the end' (note the space after the group). This special sequence can only be used to match one of the first 99 groups. If the first digit of number is 0, or number is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value number. Inside the "[" and "]" of a character class, all numeric escapes are treated as characters.

\A
Matches only at the start of the string.

\b
Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore character. Note that \b is defined as the boundary between \w and \ W, so the precise set of characters deemed to be alphanumeric depends on the values of the UNICODE and LOCALE flags. Inside a character range, \b represents the backspace character, for compatibility with Python's string literals.

\B
Matches the empty string, but only when it is not at the beginning or end of a word. This is just the opposite of \ b, so is also subject to the settings of LOCALE and UNICODE.

\d
When the UNICODE flag is not specified, matches any decimal digit; this is equivalent to the set [0-9]. With UNICODE, it will match whatever is classified as a digit in the Unicode character properties database.

\D
When the UNICODE flag is not specified, matches any non-digit character; this is equivalent to the set [^0-9]. With UNICODE, it will match anything other than character marked as digits in the Unicode character properties database.

\s
When the LOCALE and UNICODE flags are not specified, matches any whitespace character; this is equivalent to the set [ \t\n\r\f\v]. With LOCALE, it will match this set plus whatever characters are defined as space for the current locale. If UNICODE is set, this will match the characters [ \t\n\r\f\v] plus whatever is classified as space in the Unicode character properties database.

\S
When the LOCALE and UNICODE flags are not specified, matches any non-whitespace character; this is equivalent to the set [^ \t\n\r\f\v] With LOCALE, it will match any character not in this set, and not defined as space in the current locale. If UNICODE is set, this will match anything other than [ \t\n\r\f\v] and characters marked as space in the Unicode character properties database.

\w
When the LOCALE and UNICODE flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9_]. With LOCALE, it will match the set [0-9_] plus whatever characters are defined as alphanumeric for the current locale. If UNICODE is set, this will match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties database.

\W
When the LOCALE and UNICODE flags are not specified, matches any non-alphanumeric character; this is equivalent to the set [^a-zA-Z0-9_]. With LOCALE, it will match any character not in the set [0-9_], and not defined as alphanumeric for the current locale. If UNICODE is set, this will match anything other than [0-9_] and characters marked as alphanumeric in the Unicode character properties database.

\Z
Matches only at the end of the string.

Most of the standard escapes supported by Python string literals are also accepted by the regular expression parser:

\a      \b      \f      \n \r      \t      \v      \x \\ 

Octal escapes are included in a limited form: If the first digit is a 0, or if there are three octal digits, it is considered an octal escape. Otherwise, it is a group reference. As for string literals, octal escapes are always at most three digits in length.

This Tutorial is taken from python website python.org

Posix regular expression syntax

Synopsis

The POSIX-Basic regular expression syntax is used by the Unix utility sed, and variations are used by grep and emacs. You can construct POSIX basic regular expressions in Boost.Regex by passing the flag basic to the regex constructor (see syntax_option_type), for example:

// e1 is a case sensitive POSIX-Basic expression:
boost::regex e1(my_expression, boost::regex::basic);
// e2 a case insensitive POSIX-Basic expression:
boost::regex e2(my_expression, boost::regex::basic|boost::regex::icase);

POSIX Basic Syntax

In POSIX-Basic regular expressions, all characters are match themselves except for the following special characters:

.[\*^$

Wildcard:

The single character '.' when used outside of a character set will match any single character except:

* The NULL character when the flag match_no_dot_null is passed to the matching algorithms.
* The newline character when the flag match_not_dot_newline is passed to the matching algorithms.

Anchors:

A '^' character shall match the start of a line when used as the first character of an expression, or the first character of a sub-expression.

A '$' character shall match the end of a line when used as the last character of an expression, or the last character of a sub-expression.
Marked sub-expressions:

A section beginning \( and ending \) acts as a marked sub-expression. Whatever matched the sub-expression is split out in a separate field by the matching algorithms. Marked sub-expressions can also repeated, or referred-to by a back-reference.
Repeats:

Any atom (a single character, a marked sub-expression, or a character class) can be repeated with the * operator.

For example a* will match any number of letter a's repeated zero or more times (an atom repeated zero times matches an empty string), so the expression a*b will match any of the following:

b
ab
aaaaaaaab

An atom can also be repeated with a bounded repeat:

a\{n\} Matches 'a' repeated exactly n times.

a\{n,\} Matches 'a' repeated n or more times.

a\{n, m\} Matches 'a' repeated between n and m times inclusive.

For example:

^a{2,3}$

Will match either of:

aa
aaa

But neither of:

a
aaaa

It is an error to use a repeat operator, if the preceding construct can not be repeated, for example:

a(*)

Will raise an error, as there is nothing for the * operator to be applied to.
Back references:

An escape character followed by a digit n, where n is in the range 1-9, matches the same string that was matched by sub-expression n. For example the expression:

^\(a*\).*\1$

Will match the string:

aaabbaaa

But not the string:

aaabba

Character sets:

A character set is a bracket-expression starting with [ and ending with ], it defines a set of characters, and matches any single character that is a member of that set.

A bracket expression may contain any combination of the following:
Single characters:

For example [abc], will match any of the characters 'a', 'b', or 'c'.
Character ranges:

For example [a-c] will match any single character in the range 'a' to 'c'. By default, for POSIX-Basic regular expressions, a character x is within the range y to z, if it collates within that range; this results in locale specific behavior. This behavior can be turned off by unsetting the collate option flag when constructing the regular expression - in which case whether a character appears within a range is determined by comparing the code points of the characters only.
Negation:

If the bracket-expression begins with the ^ character, then it matches the complement of the characters it contains, for example [^a-c] matches any character that is not in the range a-c.
Character classes:

An expression of the form [[:name:]] matches the named character class "name", for example [[:lower:]] matches any lower case character. See character class names.
Collating Elements:

An expression of the form [[.col.] matches the collating element col. A collating element is any single character, or any sequence of characters that collates as a single unit. Collating elements may also be used as the end point of a range, for example: [[.ae.]-c] matches the character sequence "ae", plus any single character in the rangle "ae"-c, assuming that "ae" is treated as a single collating element in the current locale.

Collating elements may be used in place of escapes (which are not normally allowed inside character sets), for example [[.^.]abc] would match either one of the characters 'abc^'.

As an extension, a collating element may also be specified via its symbolic name, for example:

[[.NUL.]]

matches a 'NUL' character. See collating element names.
Equivalence classes:

An expression of theform [[=col=]], matches any character or collating element whose primary sort key is the same as that for collating element col, as with collating elements the name col may be a collating symbolic name. A primary sort key is one that ignores case, accentation, or locale-specific tailorings; so for example [[=a=]] matches any of the characters: a, À, Á, Â, Ã, Ä, Å, A, à, á, â, ã, ä and å. Unfortunately implementation of this is reliant on the platform's collation and localisation support; this feature can not be relied upon to work portably across all platforms, or even all locales on one platform.
Combinations:

All of the above can be combined in one character set declaration, for example: [[:digit:]a-c[.NUL.]].
Escapes

With the exception of the escape sequences \{, \}, \(, and \), which are documented above, an escape followed by any character matches that character. This can be used to make the special characters

.[\*^$

"ordinary". Note that the escape character loses its special meaning inside a character set, so [\^] will match either a literal '\' or a '^'.
What Gets Matched

When there is more that one way to match a regular expression, the "best" possible match is obtained using the leftmost-longest rule.
Variations

Grep

When an expression is compiled with the flag grep set, then the expression is treated as a newline separated list of POSIX-Basic expressions, a match is found if any of the expressions in the list match, for example:

boost::regex e("abc\ndef", boost::regex::grep);

will match either of the POSIX-Basic expressions "abc" or "def".

As its name suggests, this behavior is consistent with the Unix utility grep.
emacs

In addition to the POSIX-Basic features the following characters are also special:

Character

Description

+

repeats the preceding atom one or more times.

?

repeats the preceding atom zero or one times.

*?

A non-greedy version of *.

+?

A non-greedy version of +.

??

A non-greedy version of ?.

And the following escape sequences are also recognised:

Escape

Description

\|

specifies an alternative.

\(?: ... )

is a non-marking grouping construct - allows you to lexically group something without spitting out an extra sub-expression.

\w

matches any word character.

\W

matches any non-word character.

\sx

matches any character in the syntax group x, the following emacs groupings are supported: 's', ' ', '_', 'w', '.', ')', '(', '"', '\'', '>' and '<'. Refer to the emacs docs for details.

\Sx

matches any character not in the syntax grouping x.

\c and \C

These are not supported.

\`

matches zero characters only at the start of a buffer (or string being matched).

\'

matches zero characters only at the end of a buffer (or string being matched).

\b

matches zero characters at a word boundary.

\B

matches zero characters, not at a word boundary.

\<

matches zero characters only at the start of a word.

\>

matches zero characters only at the end of a word.

Finally, you should note that emacs style regular expressions are matched according to the Perl "depth first search" rules. Emacs expressions are matched this way because they contain Perl-like extensions, that do not interact well with the POSIX-style leftmost-longest rule.
Options

There are a variety of flags that may be combined with the basic and grep options when constructing the regular expression, in particular note that the newline_alt, no_char_classes, no-intervals, bk_plus_qm and bk_plus_vbar options all alter the syntax, while the collate and icase options modify how the case and locale sensitivity are to be applied.

Regular Expression patterns

Introductory Guide to regular expressions

Credits: This tutorial is written by Karen Gayda. This article is taken as it is from javascriptkit.com for learning purpose.

Introduction

Validating user input is the bane of every software developer’s existence. When you are developing cross-browser web applications (IE4+ and NS4+) this task becomes even less enjoyable due to the lack of useful intrinsic validation functions in JavaScript Fortunately, JavaScript 1.2+ has incorporated regular expressions. In this article I will present a brief tutorial on the basics of regular expressions and then give some examples of how they can be used to simplify data validation.

Regular Expressions and Patterns

Regular expressions are very powerful tools for performing pattern matches. PERL programmers and UNIX shell programmers .

have enjoyed the benefits of regular expressions for years. Once you master the pattern language, most validation tasks become trivial. You can perform complex tasks that once required lengthy procedures with just a few lines of code using regular expressions.

So how are regular expressions implemented in JavaScript? There are two ways:

1) Using literal syntax.
2) When you need to dynamically construct the regular expression, via the RegExp() constructor.

The literal syntax looks something like:

var RegularExpression = /pattern/

while the RegExp() constructor method looks like

var RegularExpression  =  new RegExp("pattern");

The RegExp() method allows you to dynamically construct the search pattern as a string, and is useful when the pattern is not known ahead of time.

To use regular expressions to validate a string you need to define a pattern string that represents the search criteria, then use a relevant string method to denote the action (ie: search, replace etc). Patterns are defined using string literal characters and metacharacters. For example, the following regular expression determines whether a string contains a valid 5-digit US postal code (for sake or simplicity, other possibilities are not considered):

  

Monday, March 1, 2010

Renaming Files with Regular Expression


Download the tool here Originally a product of 12noon..com . I thought it would be worth sharing, and hence sharing.

If you need to rename a number of files in complicated ways, RenameRegEx can do it.

RenameRegEx adds a shortcut to the Windows Explorer context menu. When you select one or more files and right-click on them, you can select the Rename RegEx… command to display the RenameRegEx window. There you can enter the pattern to match and the replacement pattern, using the full power of regular expressions.


In the target expression, you can use $1 through $9 for groups you matched in the source expression.

You can use the right-arrows after each expression field to enter any of the special characters described below. This is usually easier than memorizing the sometimes arcane syntax of regular expressions.

The dropdown combo boxes store their previous values so you can easily reuse previous expressions.

The Case-sensitive matching check box enables, of course, case-sensitive searches, which ensures that the text will only match if the case matches.

If you select the Replace all matches check box, every match will be replaced with the target expression. If the check box is cleared, only the first match will be replaced.

If the Replace "#" with… check box is selected, any occurrence of the # character in the target expression will be replaced by a number, starting with the number you enter.
Regular Expressions

This documentation will be fairly brief and it assumes you have some knowledge of regular expressions. If you want to learn more, you can read about them on the Internet.

You use a regular expression to match a specific pattern of text, such as all words that begin with “b” or all text starting with 0 and ending with a period.
Regular Expression Syntax
. Matches any single character.
[ ] Indicates a character class. Matches any character inside the brackets (for example, [abc] matches "a", "b", and "c").
^ If this metacharacter occurs at the start of a character class, it negates the character class. A negated character class matches any character except those inside the brackets (for example, [^abc] matches all characters except "a", "b", and "c").
If ^ is at the beginning of the regular expression, it matches the beginning of the input (for example, ^[abc] will only match input that begins with "a", "b", or "c").
- In a character class, indicates a range of characters (for example, [0-9] matches any of the digits "0" through "9").
? Indicates that the preceding expression is optional: it matches once or not at all (for example, [0-9][0-9]? matches "2" and "12").
+ Indicates that the preceding expression matches one or more times (for example, [0-9]+ matches "1", "13", "666", and so on).
* Indicates that the preceding expression matches zero or more times.
??, +?, *? Non-greedy versions of ?, +, and *. These match as little as possible, unlike the greedy versions which match as much as possible. Example: given the input "", <.*?> matches "" while <.*> matches "".
( ) Grouping operator. Example: (\d+,)*\d+ matches a list of numbers separated by commas (such as "1" or "1,23,456").
{ } Indicates a match group. The actual text in the input that matches the expression inside the braces can be retrieved through the CAtlREMatchContext object.
\ Escape character: interpret the next character literally (for example, [0-9]+ matches one or more digits, but [0-9]\+ matches a digit followed by a plus character). Also used for abbreviations (such as \a for any alphanumeric character; see table below).
If \ is followed by a number n, it matches the nth match group (starting from 0). Example: <{.*?}>.*? matches "Contents".
$ At the end of a regular expression, this character matches the end of the input. Example: [0-9]$ matches a digit at the end of the input.
| Alternation operator: separates two expressions, exactly one of which matches (for example, T|the matches "The" or "the").
! Negation operator: the expression following ! does not match the input. Example: a!b matches "a" not followed by "b".
Abbreviations

You can also use abbreviations, such as \d instead of [0-9].
\a Any alphanumeric character.
\b White space (blank).
\c Any alphabetic character.
\d Any decimal digit.
\h Any hexadecimal digit.
\n Newline.
\q A quoted string.
\w A simple word.
\z An integer.
Tips and Tricks

If you have a set of numbered files that you want to renumber, it’s possible that the filenames might “collide” during renaming. For example:
Current name New name
test1.txt test1.txt
test11.txt test12.txt
test2.txt test3.txt

If the source expression is test.+ and the target expression is test#.txt, RenameRegEx will display an error when it tries to rename test11.txt to test2.txt because test2.txt already exists.

The workaround is to modify the rest of the filename too, not just the numbers. In this example, you can change the target expression to test_#.txt. (Note the new underscore.)

Note: I've seen times—usually when I've selected dozens of folders, shortcuts, and files—when Windows has given Rename Regular Expression invalid locations for its menu command. In these cases, RenameRegEx will merely not add its command to the context menu. You can select fewer items and try again.
Requirements

RenameRegEx runs on Microsoft® Windows® Vista, XP Home and Professional, 2003, and 2000. (Although it may work on other versions of Windows, it’s not guaranteed.)
History

1.10: Added support for folders, shortcuts, etc. Fixed some cases where the context menu command wouldn't appear.

Here I am posting a tutorial of Regular Expressions by an expert,hope it would be useful .


**************************
Author: Jan Borsodi


Publishing date: 30.10.2000 18:02

This article will give you an introduction to the world of regular expressions. I'll start off with explaining what regular expressions are and introduce their syntax, then some examples with varying complexity.

Concept
A regular expression is a text pattern consisting of a combination of alphanumeric characters and special characters known as metacharacters. A close relative is in fact the wildcard expression which are often used in file management. The pattern is used to match against text strings. The result of a match is either successful or not, however when a match is successful not all of the pattern must match, this is explained later in the article.

You'll find that regular expressions are used in three different ways: Regular text match, search and replace and splitting. The latter is basicly the same as the reverse match i.e. everything the regular expression did not match.

Regular expressions are often simply called regexps or RE, but for consistency I'll be referring to it with its full name.

Due to the versatility of the regular expression it is widely used in text processing and parsing. UNIX users are probably familiar with them through the use of the programs, grep, sed, awk and ed. Text editors such as (X)Emacs and vi also use them heavily. Probably the most known use of regular expressions are in the programming language Perl; you'll find that Perl sports the most advanced regular expression implementation to this day.

Usage
Now you're probably wondering why you should bother to learn regular expressions. Well if you're a normal computer user your benefits from using them are somewhat small, however if you're either a developer or a system administrator you'll find that knowing regular expressions will make your (professional) life so much better.

Developers can use them to parse text files, fix up code and other wonders. System administrators can use them to search through logs, automate boring tasks and sniff the network traffic for unauthorized activity.

Actually I would go so far as to say it's a crime for a System Administrator not to have any knowledge of regular expressions.

Quantifiers
The contents of an expression are, as explained earlier, a combination of alphanumeric characters and metacharacters. An alphanumeric character is either a letter from the alphabet:

abc

or a number:

123

Actually in the world of regular expressions any character which is not a metacharacter will match itself (often called literal characters), however a lot of the time you're mostly concerned with the alphanumeric characters. A very special character is the backslash \, as this turns any metacharacters into literal characters, and alphanumeric characters into a sort of metacharacter or sequence. The metacharacters are:

\ | ( ) [ { ^ $ * + ? . < >

With that said normal characters don't sound too interesting so let's jump to our very first metacharacters.

The punctuation mark, or dot, . needs explaining first since it often leads to confusion. This character will not, as many might think, match the punctuation in a line. It is instead a special metacharacter which matches any character. Using this where you wanted to find the end of the line, or the decimal in a floating number, will lead to strange results. As explained above, you need to backslashify it to get the literal meaning. For instance take this expression:

1.23

will match the number 1.23 in a text as you might have guessed, but it will also match these next lines:

1x23
1 23
1-23

to make the expression only match the floating number we change it to:

1\.23

Remember this, it's very important. Now with that said we can get the show going.

Two heavily recurring metacharacters are:

* and +

They are called quantifiers and tell the engine to look for several occurrences of a character; the quantifier always precedes the character at hand. The * character matches zero or more occurrences of the character in a row, the + character is similar but matches one or more.

So if you decided to find words which had the character c in it you might be tempted to write:

c*

What might come as a surprise to you is that you will find an enormous amount of matches, even words with no c in them will match. "How is this so?" you ask. Well, the answer is simple. Recall that the * character matches zero or more characters, and that's exactly what you matched, zero characters.

You see, in regular expressions you have the possibility to match what is called the empty string, which is simply a string with zero size. This empty string can actually be found in all texts. For instance the word:

go

contains three empty strings. They are contained at the position right before the g, in between the g and the o and after the o. And an empty string contains exactly one empty string. At first this might seem like a really silly thing to do, but you'll learn later on how this is used in more complex expressions.

So with this knowledge we might want to change our expression to:

c+

and voila we get only words with c in them.

The next metacharacter you'll learn is:

?

This simply tells the engine to either match the character or not (zero or one). For instance the expression:

cows?

will match any of these lines:

cow
cows

These three metacharacters are simply a specialized scenario for the more generalized quantifier:

{n,m}

the n and m are respectively the minimum and maximum size for the quantifier. For instance:

{1,5}

means match one or up to five characters. You can also skip m to allow for infinite match:

{1,}

which matches one or more characters. This is exactly what the + character does. So now you see the connection, * is equal to {0,}, + is equal to {1,} and ? is equal to {0,1}.

The last thing you can do with the quantifier is to also skip the comma:

{5}

which means to match 5 characters, no more, no less.

Assertions
The next type of metacharacters is assertions. These will match if a given assertion is true. The first pair of assertions are:

^ and $

which match the beginning of the line, and the end of the line, respectively. Note that some regular expression implementations allows you to change their behavior so that they will instead match the beginning of the text and the end of the text. These assertions always match a zero length string, or in other words, they match a position. For instance, if you wrote this expression:

^The

it would match any line which began with the word The.

The next assertion characters match at the beginning and end of a word; they are:

< and >

they come in handy when you want to match a word precisely. For instance:

cow

would match any of the following words:

cow
coward
cowage
cowboy
cowl

A small change to the expression:



and you'll only match the word cow in the text.

One last thing to be said is that all literal characters are in fact assertions themselves. The difference between them and the ones above is that literal characters have a size. So for cleanliness sake we only use the word "assertions" for those that are zero-width.

Groups and Alternation
One thing you might have noticed when we explained quantifiers is that they only worked on the character to the left. Since this pretty much limits our expressions I'll explain other uses for quantifiers. Quantifiers can also be used on metacharacters; using them on assertions is silly since assertions are zero-width and matching one, two, three or more of them doesn't do any good. However the grouping and sequence metacharacters are perfect for being quantified. Let's first start with grouping.

You can form groups, or subexpressions as they are frequently called, by using the begin and end parenthesis characters:

( and )

The ( starts the subexpression and the ) ends it. It is also possible to have one or more subexpressions inside a subexpressions. The subexpression will match if the contents match. So mixing this with quantifiers and assertions you can do:

( ?ho)+

which matches all of the following lines:

ho
ho ho
ho ho ho
hohoho

Another use for subexpressions are to extract a portion of the match if it matches. This is often used in conjunction with sequences, which are discussed later.

You can also use the result of a subexpression for what is called a back reference. A back reference is given by using a backslashified digit, a single non-zero digit. This leaves you with nine back references (0 through 9). The back reference matches whatever the corresponding subexpression actually matched (except that {article_contents_1} matches a null character). To find the number of the subexpression, count the open parentheses from the left.

The uses for back references are somewhat limited, especially since you only have nine of them, but on some rare occasion you might need it. Note some regular expression implementations can use multi-digit numbers as long as they don't start with a 0.

Next are alternations, which allow you to match on any of many words. The alternation character is:

|

A sample usage is:

Bill|Linus|Steve|Larry

would match either Bill, Linus, Steve or Larry. Mixing this with subexpressions and quantifiers we can do:

cow(ard|age|boy|l)?

which matches any of the following words but no others:

cow
coward
cowage
cowboy
cowl

I mentioned earlier in the article that not all of the expression must match for the match to be successful. This can happen when you're using subexpressions together with alternations. For example:

((Donald|Dolly) Duck)|(Scrooge McDuck)

As you see only the left or right top subexpression will match, not both. This is sometimes handy when you want to run a complex pattern in one subexpression and if it fails try another one.

Sequences
Last we have sequences, which define sequences of characters which can match. Sometimes you don't want to match a word directly but rather something that resembles one. The sequence characters are:

[ and ]

Any characters put inside the sequence brackets are treated as literal characters, even metacharacters. The only special characters are - which denotes character ranges, and ^ which is used to negate a sequence. The sequence is somewhat similar to alternation; the similarity is that only one of the items listed will match. For instance:

[a-z]

will match any lowercase characters which are in the English alphabet (a to z). Another common sequence is:

[a-zA-Z0-9]

Which matches any lowercase or capital characters in the English alphabet as well as numbers. Sequences are also mixed with quantifiers and assertions to produce more elaborate searches. Example:

<[a-zA-Z]+>

matches all whole words. This will match:

cow
Linus
regular
expression

but will not match:

200
x-files
C++

Now if you wanted to find anything but words, the expression:

[^a-zA-Z0-9]+

would find any sequences of characters which do not contain the English alphabet or any numbers.

Some implementations of regular expressions allow you to use shorthand versions for commonly used sequences, they are:

\d, a digit ([0-9])
\D, a non-digit ([^0-9])
\w, a word (alphanumeric) ([a-zA-Z0-9])
\W, a non-word ([^a-zA-Z0-9])
\s, a whitespace ([ \t\n\r\f])
\S, a non-whitespace ([^ \t\n\r\f])

Wildcards
For people who have some knowledge with wildcards (also known as file globs or file globbing), I'll give a brief explanation on how to convert them to regular expressions. After reading this article, you probably have seen the similarities with wildcards. For instance:

*.jpg

matches any text which end with .jpg. You can also specify brackets with characters, as in:

*.[ch]pp

matches any text which ends in .cpp or .hpp. Altogether very similar to regular expressions.

The * means match zero or more of anything in wildcards. As we learned, we do this is regular expression with the punctuation mark and the * quantifier. This gives:

.*

Also remember to convert any punctuation marks from wildcards to be backslashified.

The ? means match any character but do match something. This is exactly what the punctuation mark does.

Square brackets can be used untouched since they have the same meaning going from wildcards to regular expressions.

These leaves us with:

* Replace any * characters with .*
* Replace any ? characters with .
* Leave square brackets as they are.
* Replace any characters which are metacharacters with a backslashified version.

Examples

*.jpg

would be converted to:

.*\.jpg

ez*.[ch]pp

would convert to:

ez.*\.[ch]pp

or alternatively:

ez.*\.(cpp|hpp)

Example Regular Expressions
To really get to know regular expressions I've left some commonly used expressions on this page. Study them, experiment and try to understand exactly what they are doing.

Email validity: will only match email addresses which are valid, such as "user@host.com":

[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+

Email validity #2: matches email addresses with a name in front, like "John Doe ":

("?[a-zA-Z]+"?[ \t]*)+\<[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+\>

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:

//.+$

C/C++ span line comments (it has one flaw, can you spot it?):

/\*[^*]*\*/

Floating point numbers: matches simple floating point numbers of the kind 1.2 and 0.5:

-?[0-9]+\.[0-9]+

Hexadecimal numbers: matches C/C++ style hex numbers, e.g. 0xcafebabe:

0x[0-9a-fA-F]+

Found at http://publish.ez.no/article/articleprint/11/