Regular Expressions

Regular Expressions (RegEx or RegExp)

A Regular expression (sometimes called a rational expression) is a string that describes a search pattern used when working with text. Regular expressions can be used with grep (the global regular expression parser) and sed (stream editor) and also used in shell scripting , Perl, Java, python and so on.

A Regular Expression contains one or more of the following:

(.) dot

It will matches any single character and equivalent to ? (question mark) in standard wildcard expressions.

\ (backslash)

backslash is the escape character i.e. to protect a subsequent special character. Thus, “\\” searches for a backslash. Note you may need to use quotation marks and backslash(es).

(.*) (dot and asterisk)

Used to match any string and equivalent to * in standard wildcards.

* (asterisk)

The preceding item will be matched zero or more times.

{N}

The preceding item is matched exactly N times.

{N,}

The preceding item is matched N or more times.

{N,M}

The preceding item is matched at least N times, but not more than M times.

^ (caret)

It means “the beginning of the line”. So “^a” means find a line starting with an “a”.

$ (dollar sign)

It means the end of the line”. So “a$” means find a line ending with an “a”.

– (hyphen)

Used to represent range.

| (pipe )

This makes a logical OR relationship between wildcards.

 

Advertisements