Regular Expressions

Basic knowledge you need when dealing with the REPhoto by XPS on UnsplashA regular expression is a searching pattern that contains a sequence of characters. Regular expressions are useful for extracting information from a text by searching for one or m…


This content originally appeared on Level Up Coding - Medium and was authored by Prathibha Perera

Basic knowledge you need when dealing with the RE

Photo by XPS on Unsplash
A regular expression is a searching pattern that contains a sequence of characters. Regular expressions are useful for extracting information from a text by searching for one or more matches of a specific search pattern. When we are working with regular expressions there are common things that are needed to deal with. They can be categorized as follows.
  1. Basic pattern
  2. [] operator
  3. () operator
  4. Character classes
  5. Negation
  6. ^ and $
  7. OR operator - |
  8. Operator based (*, +, ?, .)
  9. Numerical range
  10. Boundaries

I use regexr.com to practice regular expressions. You can practice one by one to get the basic knowledge about the RE as follows.

1. Basic pattern

It is the simplest regular expression form that is the character sequence itself, used to match a sequence of characters within a given sentence or paragraph.

With the global flag, then only matches the given word
With the case-insensitive flag matches the word without case sensitivity

2. [] Operator

Square braces specify a disjunction of characters to match

Match either Apple or apple
Match the letter ‘x’ or ‘y’ or ‘z’
Match any digit

Dash (-) with [] is used to specify any one character in a range

A single-digit will be matched

3. () Operator

It constructs a grouping construct to establish a precedence order and parenthesis is also useful for OR’ing two expressions with the bar | character.

() with | operator

4. Character classes

The following table has mentioned the basic character classes that are useful for you to deal with RE.

+---------------+----------------------------------------------+
| Metacharacter | Behavior |
+---------------+----------------------------------------------+
| \d | Match any digit character [0-9] |
| \D | Match any non-digit character |
| \n | Match a new line |
| \s | Match white spaces |
| \S | Match non-white spaces |
| \w | Match word characters (includes a-z and 0–9) |
| \W | Match non-word characters |
| | |
+---------------+----------------------------------------------+
Match any digit character [0–9]
Match any non-digit character
Match white spaces
Match non-white spaces
Match word characters (includes a-z and 0–9)
Match non-word characters

5. Negation

If the ^ caret symbol is the first symbol after opening the square brace, it specifies the negation operation.

^ specifies that not an upper case letter

6. ^ and $

^ and $ can be called as most common anchors in regular expressions. ^ matches the start of a line and $ matches the end of the line.

Here examples match with the multiline flag enabled.

^ Specifies the start of the sentence
$ Specifies the end of the sentence
Both ^ and $ have been used to specify the whole sentence “Apple is the world’s 4th-largest PC vendor.”

7. OR Operator — |

The OR operator allows us to specify different allowable capture groups

Match both “4th-largest” and “fourth-largest”

8. Operator based ( *, + , ?, . )

*, +? and . are the operators that are used to deal with RE.

It should exactly not existence of the letter “u” or the existence of “u”
It should have zero or more occurrences of the character “o”
It should have exactly one or more occurrences of the character “o”
It should have zero or more occurrences of the “h” with “oohh”
Between “r” and “n” it can have different letters but only one letter

9. Numerical range

It is used to specify a quantity range using {low, high}

{n,m} — minimum is n and maximum is m

Minimum should have 4 digits and maximum should have 6 letters

{n,} — minimum is n and maximum is undefined

Minimum should have 4 digits and the maximum is undefined

10. Boundaries

\b matches the word boundary and \B is used to identify non-word boundary

Matches with “the” either “The” or “the” but do not match the word “other”
Matches with “the” as a non-word boundary and not match for word “the”

OK Cool, Guys 🤗🤗🤗🤗

This is the end of the Regular Expression article and I hope you may grab some important facts about RE. If you enjoy the article and help to improve your knowledge on RE, then hit a clap. 🙌

Thank you for reading !!! 😊


Regular Expressions was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Prathibha Perera


Print Share Comment Cite Upload Translate Updates
APA

Prathibha Perera | Sciencx (2021-11-04T04:20:58+00:00) Regular Expressions. Retrieved from https://www.scien.cx/2021/11/04/regular-expressions/

MLA
" » Regular Expressions." Prathibha Perera | Sciencx - Thursday November 4, 2021, https://www.scien.cx/2021/11/04/regular-expressions/
HARVARD
Prathibha Perera | Sciencx Thursday November 4, 2021 » Regular Expressions., viewed ,<https://www.scien.cx/2021/11/04/regular-expressions/>
VANCOUVER
Prathibha Perera | Sciencx - » Regular Expressions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/04/regular-expressions/
CHICAGO
" » Regular Expressions." Prathibha Perera | Sciencx - Accessed . https://www.scien.cx/2021/11/04/regular-expressions/
IEEE
" » Regular Expressions." Prathibha Perera | Sciencx [Online]. Available: https://www.scien.cx/2021/11/04/regular-expressions/. [Accessed: ]
rf:citation
» Regular Expressions | Prathibha Perera | Sciencx | https://www.scien.cx/2021/11/04/regular-expressions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.