Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Regular Expressions - Introduction


May 28, 2021 Regular expression


Table of contents


Regular Expression - Introduction

Unless you have used regular expressions before, you may not be familiar with the term. However, there is no doubt that you have used some regular expression concepts that do not involve scripts.

For example, you are likely to use ? a nd a wildcard to find files on your hard drive. W ildcards match individual characters in the file name, while wildcards match zero or more characters. L ike data?. A pattern like dat looks for the following files:

data1.dat
data2.dat
datax.dat
dataN.dat

Use the character instead of ? C haracters expand the number of files found. Data*.dat all of the following files:

data.dat
data1.dat
data2.dat
data12.dat
datax.dat
dataXYZ.dat

Although this search method is useful, it is limited. The concept on which regular expressions depend is introduced by understanding how the wildcard works, but regular expressions are more powerful and flexible.

Regular expressions can be used in a simple way to achieve powerful functionality. Here's a simple example:

 ^.+@.+\\..+$ 

Continuing with this tutorial will give you the freedom to apply this code.

Why use regular expressions?

Typical search and replace operations require you to provide exact text that matches the expected search results. While this technique may be sufficient for performing simple search and replace tasks for static text, it is inflexible and can be difficult, if not impossible, to search for dynamic text in this way.

By using regular expressions, you can:

  • Test the pattern within the string.
    For example, you can test the input string to see if phone number mode or credit card number mode appears within the string. This is called data validation.
  • Replace the text.
    You can use regular expressions to identify specific text in a document, delete it entirely, or replace it with other text.
  • Extract substrings from strings based on pattern matching.
    You can find specific text within a document or input field.

For example, you might want to search the entire site, remove outdated material, and replace some HTML formatting tags. I n this case, you can use regular expressions to determine whether the material or the HTML-formatted tag appears in each file. T his process narrows the list of affected files to those that contain material that needs to be deleted or changed. Y ou can then use regular expressions to remove outdated material. Finally, you can use regular expressions to search for and replace tags.

History

The "ancestors" of regular expressions can be traced back to early studies of how the human nervous system works. Two neurophysiologists, Warren McCulloch and Walter Pitts, have developed a mathematical way to describe these neural networks.

In 1956, a mathematician named Stephen Kleene, based on the early work of McCulloch and Pitts, published a paper entitled "The Notation of Neural Network Events", introducing the concept of regular expressions. Regular expressions are expressions that describe what he calls "alge of regular sets" and therefore use the term regular expressions.

It was then discovered that this work could be applied to some early research using Ken Thompson, the main inventor of Unix, using the computational search algorithm. The first utility application for regular expressions is the qed editor in Unix.

What remains, as they say, is well-known history. Regular expressions have been an important part of text-based editors and search tools ever since.

Applications

At present, regular expressions have been widely used in many software, including operating systems such as nix (Linux, Unix, etc.), HP, PHP, C#, Java and many other development environments, as well as many applications, you can see the shadow of regular expressions.

The regular expression of C#

In our C# tutorial, this section of the chapter on C# regular expressions is devoted to the knowledge of C# regular expressions.

Java regular expression

In our Java tutorial, this section of Java Regular Expressions is devoted to knowledge of Java regular expressions.

JavaScript regular expression

In our JavaScript tutorial, this section of JavaScript RegExp Objects focuses on JavaScript regular expressions, and we also provide a complete JavaScript RegExp object reference manual.

Python regular expression

In our Python basic tutorial, the Python Regular Expressions section is devoted to knowledge of Python regular expressions.

Ruby regular expression

In our Ruby tutorial, ruby regular expressions This section is devoted to knowledge of Ruby regular expressions.