# Regex **Regular expression:** Regular expression is a pattern that matches against text. ![](https://i1.wp.com/digitalfortress.tech/wp-content/uploads/2018/05/regex1.png?fit=526%2C526&ssl=1) --- ## **What we need to understand:** There are a lot of such examples like URL, Phone numbers, working hours of an employee in a company and many more. So, if we are able to find a pattern out of some given text, then we can develop an expression to extract the data that matched out of whole text and that expression we call Regular Expression. ![](https://media.giphy.com/media/xUPGcmF2iGsTGEFVL2/giphy.gif) --- ## Anchors ^The --- matches any string that starts with "The" end$ --- matches a string that ends with "end" ^The end$ --- exact string match (starts and ends with "The end") --- ## Quantifiers — * + ? and {} abc* --- matches a string that has "ab" followed by zero or more "c" abc+ --- matches a string that has "ab" followed by one or more "c" abc? --- matches a string that has "ab" followed by zero or one "c" abc{2} --- matches a string that has "ab" followed by 2 "c" abc{2,} --- matches a string that has "ab" followed by 2 or more "c" abc{2,5} --- matches a string that has "ab" followed by 2 up to 5 "c" --- ## OR operator — | or [] a(b|c) --- matches a string that has "a" followed by "b" or "c" a[bc] --- same as previous --- ## Character classes — \d \w \s and . \d --- matches a single character that is a digit \w --- matches a word character (alphanumeric character plus underscore) \s --- matches a whitespace character (includes tabs and line breaks) . --- matches any character \D --- matches a single non-digit character --- ## Flags A regex usually comes within this form /abc/, where the search pattern is delimited by two slash characters /. At the end we can specify a flag with these values (we can also combine them each other): g --- (global) does not return after the first match, restarting the subsequent searches from the end of the previous match m --- (multi-line) when enabled ^ and $ will match the start and end of a line, instead of the whole string i --- (insensitive) makes the whole expression case-insensitive (for instance /aBc/i would match AbC) --- ## Bracket expressions — [] [a-z] --- a string that accepts only lower case letters [0-9] --- a string that has a character from 0 to 9 [^a-zA-Z] --- a string that has not a letter from a to z or from A to Z. In this case the ^ is used as negation of the expression. --- ## Grouping Constructs in Regular Expressions Grouping constructs delineate the subexpressions of a regular expression and capture the substrings of an input string. You can use grouping constructs to do the following: 1.Match a subexpression that is repeated in the input string. 2.Apply a quantifier to a subexpression that has multiple regular expression language elements. 3.Include a subexpression in the string that is returned by the Regex.Replace and Match.Result methods. 4.Retrieve individual subexpressions from the Match.Groups property and process them separately from the matched text as a whole. --- ## Email Valdiator ``` /^[A-Za-z]{1}[A-Za-z\d\._]+@([A-Za-z]{3,})\.([a-zA-Z]{2,})$/ ``` ![](https://www.howtogeek.com/wp-content/uploads/2019/04/email-bomb.jpg) --- ## A useful website to learn REGEX: WWW.REGEXR.COM ![](https://i.imgur.com/FCOHQK1.png) --- ## Thanks For Listening! ![](https://media.giphy.com/media/3otPoUkg3hBxQKRJ7y/giphy.gif)