14.2. Regular expressions-introduction

发布时间 :2025-10-25 12:24:09 UTC      

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

例如,您很可能使用 ? 和 * 通配符来查找硬盘上的文件。? 通配符匹配文件名中的 0 个或 1 个字符,而 * 通配符匹配零个或多个字符。像 data(w)?.dat 这样的模式将查找下列文件:

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

Use* 字符代替 ? 字符扩大了找到的文件的数量。data.* . Dat matches all the following files:

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

Although this search method is useful, it is limited. By understanding how wildcards work, the concepts that regular expressions depend on are introduced, but regular expressions are more powerful and more flexible.

The use of regular expressions can achieve powerful functions in a simple way. Here’s a simple example:

image0

  • The starting position of the input string for the match.

  • [0-9] + match multiple digits [0-9] Match a single number, + match one or more.

  • Abc$ matches the letter abc and ends with abc, with $as the end of the matching input string.

When we write the user registration form, only allow the user name to contain characters, numbers, underscores and connection characters-and set the length of the user name, we can use the following regular expression to set it.

image1

The above regular expressions can match runoob、runoob1、run-oob、run_oob But it doesn’t match. ru Because it contains letters that are too short to match with less than three It doesn’t match. runoob$ Because it contains special characters

14.2.1. Example

Matches a string that begins with a number and ends with abc. :

varstr="123abc";varpatt1=
/^[0-9]+abc$/;document.write(str.match(patt1));

The text of the following tag is the obtained matching expression:

123abc

尝试一下 »

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

14.2.2. Why use regular expressions?

A typical search and replace operation requires you to provide exact text that matches the expected search results. While this technique may be sufficient for performing simple search and replacement tasks on static text, it lacks flexibility, and it can at least become 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 a phone number pattern or a credit card number pattern 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 the text completely, or replace it with other text.

  • Extract a substring from a string based on pattern matching. You can find specific text within the document or in the input field.

For example, you may need to search the entire site, remove obsolete materials, and replace some HTML format tags. In this case, you can use regular expressions to determine whether the material or the HTML format tag appears in each file. This procedure narrows the list of affected files to those that contain materials that need to be deleted or changed. You can then use regular expressions to remove obsolete materials. Finally, you can use regular expressions to search for and replace tags.

14.2.3. History of development

The “ancestor” of canonical 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 “representation of neural network events”, which introduced the concept of regular expressions. Regular expressions are used to describe what he calls “algebra of regular sets”, so the term “regular expressions” is adopted.

Subsequently, it was found that this work could be applied to some early research on computational search algorithms using Ken Thompson, the main inventor of Unix. The first utility for regular expressions is the qed editor in Unix.

As they say, the rest is well-known history. Regular expressions have been an important part of text-based editors and search tools since then.

14.2.4. Application field

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

14.2.5. C # regular expression

In our C# tutorial C# 正则表达式 This chapter is devoted to the knowledge of C # regular expressions.

14.2.6. Java regular expression

In our Java tutorial Java 正则表达式 This chapter is devoted to the knowledge of Java regular expressions.

14.2.7. JavaScript regular expression

In our JavaScript tutorial JavaScript RegExp 对象 This chapter is devoted to the knowledge of JavaScript regular expressions, and we also provide a complete JavaScript RegExp 对象参考手册 .

14.2.8. Python regular expression

In our basic Python tutorial Python 正则表达式 This chapter is devoted to the knowledge of Python regular expressions.

14.2.9. Ruby regular expression

In our Ruby tutorial Ruby 正则表达式 This chapter is devoted to the knowledge of Ruby regular expressions.

Command or environment

.

[ ]

^

$

()

\}

?


(.)

Vi

Visual C++

Awk

Awk supports this syntax by adding the parameter– posix or– re-interval on the command line. You can see the interval expression in man awk.

Sed

Delphi

Python

Java

Javascript

Php

Perl

C#

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.