Understanding regular expressions from a programming language perspective

Program code is an abstraction of the processing logic of real things, while regular expressions are a further abstraction of complex character matching program codes; that is to say, highly concise regular expressions can be considered to correspond to characters behind them. Matching program code, and character matching program code, correspond to the character matching processing logic behind it.

Character matching processing logic can be abstracted into character matching program code; character matching program code can be further abstracted into highly concise regular expressions.

Compared with character matching program code, regular expressions do not need to use keywords such as if, then, else, etc., nor do they need to call functions such as find and showMessage. These are already implicit in regular expressions. In other words, the regular expression parsing engine will automatically parse according to similar logic, which is the embodiment of the "high conciseness" of regular expressions.

The development history of programming languages:

Programmers are mainly responsible for analyzing problems and using fourth-generation languages ​​to describe problems without spending a lot of time thinking about specific processing logic and algorithm implementation. The processing logic and algorithm implementation are implemented by a compiler (Compiler) or an interpreter (Interpreter). ) Such a language parsing engine is responsible.

Programming Paradigm refers to the basic style and paradigm model in computer programming. It is the worldview and methodology that programmers consciously or unconsciously adopt in the program virtual world they create.

Common programming paradigms generally include: imperative, declarative (including functional, logical, etc.), object-oriented, generic, concurrent, aspect, etc. Each programming paradigm guides programmers to analyze and solve programming problems according to their unique tendencies (i.e. worldview) and ideas (i.e. methodology).

1. Imperative programming

The imperative programming paradigm mainly simulates the process of computer computing. Furthermore, it directly simulates the computing process of the current mainstream Von Neumann Machine and is an abstraction of the operating mechanism of the Von Neumann Machine.

The basic characteristic of the von Neumann machine is that under the centralized control of the program counter, instructions and data are obtained from the memory in sequence and then executed, so it works in a control-driven control flow manner. Corresponding to the von Neumann machine are data flow machines that work in a data-driven data flow manner, and reduction machines that work in a demand-driven data flow manner.

A program that adopts the imperative programming paradigm is an ordered instruction list composed of several action instructions, that is, it is composed of a series of imperative sentences that specify the order of execution - "First do this, then do that". It is action-oriented and emphasizes is to define the solution to the problem - that is, "how to do it", so the algorithm is explicit and the goal is implicit. So, from a programming paradigm perspective:

  • The worldview of imperative programming is: a program is an ordered list composed of several action instructions;
  • The methodology of imperative programming is to use variables to store data and statements to execute instructions.

2. Declarative programming paradigm

The declarative programming paradigm mainly simulates the thinking process of the human brain. The declarative approach emphasizes goals and ignores processes, focusing on the analysis and expression of problems rather than algorithm implementation. It does not need to specify the order of execution and is goal-oriented. It emphasizes the description of the definition problem-that is, "what to do", so the goal is explicit and the algorithm is implicit.

From a programming paradigm perspective: the worldview of declarative programming is:

  • A program is an ordered list of several target tasks;
  • The methodology of declarative programming is to use grammatical elements to describe tasks, which are converted into instructions and executed by the parsing engine.

Representative languages ​​for declarative programming are SQL, SAS, SPSS, LaTeX, Regex (regular expressions), etc.

From the perspective of programming paradigm, regularization actually belongs to the declarative programming paradigm, and is a specialized language dedicated to processing text search and matching in this specific field.

The grammatical elements in regular expressions—metacharacters, metaescape sequences, and special structures—can be understood as the embodiment of specific program logic and algorithms. Note that the meta-escape sequence mentioned here refers to the ordinary escape sequence that escapes metacharacters into their original meaning.

This article is August Day30 study notes. The content comes from Geek Time's "Introduction to Regular Expressions Course". This course is recommended.

Guess you like

Origin blog.csdn.net/key_3_feng/article/details/132591285