Regular Expression
Regular Expression
Regular Expression
• A Regular Expression (RegEx) is a sequence of characters that defines
a search pattern.
• Ex: ^a…s$
.
• The above code defines a RegEx pattern. The pattern is: any five letter
string starting with a and ending with s.
• To specify regular expressions, metacharacters are used. In the above
example, ^ and $ are metacharacters .
• Metacharacters are characters that are interpreted in a special way by a RegEx
engine. Here's a list of metacharacters:
[] . ^ $ * + ? {} () \ |
• [] - Square brackets specifies a set of characters you wish to match.
• [abc] will match if the string you are trying to match contains any of
the a, b or c.
• You can also specify a range of characters using - inside square brackets.
[a-e] is the same as [abcde].
[1-4] is the same as [1234].
[0-39] is the same as [01239].
• You can complement (invert) the character set by using caret ^ symbol at the
start of a square-bracket.
[^abc] means any character except a or b or c.
[^0-9] means any non-digit character.
• special character is the period (/./), a wildcard expression that
matches any single character (except a carriage return)