Regex Examples and How to Use Them

Check out my REGEX COOKBOOK article about the most commonly used (and most wanted) regex

Regular expressions (regex or regexp) are extremely useful in extracting information from any text by searching for one or more matches of a specific search pattern (i.e. a specific sequence of ASCII…

Factory Mind is a young and dynamic cooperative consisting of a team of passionate developers, with a kick for computer science, technology and innovation. The main goal of the cooperative is striving to become a center of excellence and a reference point for software development

Basic Examples
Regex Matches any string that
[^i*&[email protected]] contains any character other than an i, asterisk, ampersand, 2, or at-sign.
//[^rn]*[rn] contains a Java or C# slash-slash comment
^dog begins with “dog”
dog$ ends with “dog”

Learn Regular Expressions In 20 Minutes

What is a regex?

A regex can be created for a specific use or document, but some regexes can apply to almost any text or program. Here are a few examples of commonly used regex types:

1. Flags

A flag is a modifier that allows you to define your matched results. Youd add the flag after the final forward slash of the regex. Two of the most common flags are:

For example, if you were to add the flag “gi” after the final forward slash of the regex /rad/, it would return multiple matches for strings that include either “rad” or “RAD.”

2. Character sets

A character set refers to a regular expression that returns any matches that align with a defined combination of characters. For example, imagine that you want to search for strings that include either “rad,” “sad” and “bad.” Rather than search for each one separately, you could use a character set to identify every instance of each pattern. The only variable in the patterns is the first letter, so youd place all of the initial characters within brackets, followed by the characters “ad.” The resulting regex would read, “[rsb]ad.”

3. Ranges

A range refers to a collection of characters with defined starting points and endpoints. A regex range appears in brackets, like a character set, but elides characters with a hyphen. For example, if you wanted to search for a set that included the numbers 1 through 9, you could use a range instead of inputting each character individually within the brackets. Therefore, if you searched the expression “[1-9],” it would return matches that included any of the numerals within that range.

You can also search for multiple types of ranges simultaneously. For example, the expression “[a-d1-5]” would return results that include the letters “a,” “b,” “c” and “d” along with the numerals 1, 2, 3, 4 and 5. A hypothetical example of a matching string might be something like “Brad played basketball with 3 people at 4 oclock.” The letter “a” appears in the first three words and the word “at,” the letter “d” appears in the first two words and the numbers 3 and 4 also appear in the sentence.

4. Specific character counts

If you wanted to match strings with a specific number of characters, the fastest method would be to use curly brackets and a numeral to further define your expression. For example, imagine that you want to match strings that contain five consecutive letter “b”s. The expression youd use would be “b{5}.”

You can use the same sort of expression to match words or numeral strings with a specified number of characters in them. The expression “[a-z]{8},” for example, would match any word with eight letters, while the expression “[a-z]{8,14}” would match any word that has between eight and fourteen letters. The latter instance could be useful in situations such as password validation.

5. Metacharacters

A metacharacter, which consists of a backward slash and a single letter, allows you to streamline your regular expressions. Here are four common regex metacharacters:

By combining metacharacters with other regular expressions, you can exercise increased control over the returns you get. For example, if you wanted to search for phone numbers, you could use the “d” metacharacter paired with curly brackets containing the number 10, as all U.S. phone numbers have seven numbers plus a three-digit area code. In that instance, the regex would read, “d{10|.”

6. Special characters

Special characters are similar to metacharacters except they operate within the search terms. Some common special characters are:

For example, imagine that youve inputted the search term “b?atter.” The character directly preceding the term is “b,” making it an optional search element. Therefore, the returned matches may include the words “latter,” “matter” and “chatter” in addition to “batter.”

7. Alternative characters

When you want to match multiple alternative strings, you can insert the pipe symbol—a vertical line—between terms. The addition of this glyph serves the same function that it would as a search engine operator. That is, it effectively says “find this or that.” For example, if you input the regex “word|ward,” youd match with every string that includes either combination of characters. Therefore, the terms “foreword” and “forward” could both be potential matches.

4 uses for regexes

While most regexes involve different and specific directions for creation and use, some general tips can apply to all regexes. Here are a few tips for creating and using a regex:

FAQ

What is example of regular expression?

It means “Match zero or one of the group preceding this question mark.” It can also be interpreted as the part preceding the question mark is optional. In above example ‘?’ indicates that the two digits preceding it are optional. They may not occur or occur at the most once.

What is ‘?’ In regex?

You construct a regular expression in one of two ways:
  1. Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows: const re = /ab+c/; …
  2. Or calling the constructor function of the RegExp object, as follows: const re = new RegExp(‘ab+c’);

How do I use regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group. For more information about numbered capturing groups, see Grouping Constructs. All digits that follow $ are interpreted as belonging to the number group.

What is $1 regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group. For more information about numbered capturing groups, see Grouping Constructs. All digits that follow $ are interpreted as belonging to the number group.

Related Posts

Leave a Reply

Your email address will not be published.