0% found this document useful (0 votes)
85 views

How To Write Regular Expressions?: What Is A Regular Expression and What Makes It So Important?

Regular expressions are patterns used to match character sequences and perform search and replace operations. They are used for tasks like validating email addresses or extracting URL parameters. Regular expressions use special characters like *, +, ?, and {} to define repeaters and wildcards. Learning how to construct regular expressions involves understanding how these special characters work to match varying patterns of text flexibly.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

How To Write Regular Expressions?: What Is A Regular Expression and What Makes It So Important?

Regular expressions are patterns used to match character sequences and perform search and replace operations. They are used for tasks like validating email addresses or extracting URL parameters. Regular expressions use special characters like *, +, ?, and {} to define repeaters and wildcards. Learning how to construct regular expressions involves understanding how these special characters work to match varying patterns of text flexibly.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

How to write Regular Expressions?

A regular expression (sometimes called a rational expression) is a sequence of


characters that define a search pattern, mainly for use in pattern matching with strings,
or string matching, i.e. “find and replace”-like operations.(Wikipedia).
Regular expressions are a generalized way to match patterns with sequences of
characters. It is used in every programming language like C++, Java and Python.
What is a regular expression and what makes it so important?
Regex are used in Google analytics in URL matching in supporting search and replace
in most popular editors like Sublime, Notepad++, Brackets, Google Docs and Microsoft
word.
Example : Regular expression for an email address :
^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$
The above regular expression can be used for checking if a given set of characters is an
email address or not.
How to write regular expression?

 Repeaters : * , + and { } :
These symbols act as repeaters and tell the computer that the preceding character
is to be used for more than just one time.
 The asterisk symbol ( * ):
It tells the computer to match the preceding character (or set of characters) for 0 or
more times (upto infinite).
 Example : The regular expression ab*c will give ac, abc, abbc,
abbbc….ans so on
 The Plus symbol ( + ):
It tells the computer to repeat the preceding character (or set of characters) for
atleast one or more times(upto infinite).
 Example : The regular expression ab+c will give abc, abbc,
abbc, … and so on.
 The curly braces {…}:
It tells the computer to repeat the preceding character (or set of characters) for as
many times as the value inside this bracket.
 Example : {2} means that the preceding character is to be repeated 2
 times, {min,} means the preceding character is matches min or more
 times. {min,max} means that the preceding character is repeated at
 least min & at most max times.
 Wildcard – ( . )
The dot symbol can take place of any other symbol, that is why it
is called the wildcard character.
 Example :
 The Regular expression .* will tell the computer that any character
can be used any number of times.
 Optional character – ( ? )
This symbol tells the computer that the preceding character may
or may not be present in the string to be matched.
 Example :
 We may write the format for document file as – “docx?”
 The ‘?’ tells the computer that x may or may not be
present in the name of file format.
 The caret ( ^ ) symbol: Setting position for match :tells the computer that the
match must start at the beginning of the string or line.
Example : ^\d{3} will match with patterns like "901" in "901-333-".
 The dollar ( $ ) symbol
It tells the computer that the match must occur at the end of the string or before \n
at the end of the line or string.
Example : -\d{3}$ will match with patterns like "-333" in "-901-
333".
 Character Classes
A character class matches any one of a set of characters. It is used to match the
most basic element of a language like a letter, a digit, space, a symbol etc.
/s : matches any whitespace characters such as space and tab
/S : matches any non-whitespace characters
/d : matches any digit character
/D : matches any non-digit characters
/w : matches any word character (basically alpha-numeric)
/W : matches any non-word character
/b : matches any word boundary (this would include spaces, dashes, commas,
semi-colons, etc)
[set_of_characters] – Matches any single character in set_of_characters. By
default, the match is case-sensitive.
Example : [abc] will match characters a,b and c in any string.
[^set_of_characters] – Negation: Matches any single character that is not in
set_of_characters. By default, the match is case sensitive.

You might also like