search
HomeBackend DevelopmentPHP TutorialPHP POSIX regular expressions

PHP POSIX regular expressions

Dec 04, 2019 am 10:31 AM
posix

1 Basic knowledge

Regular expression is a way to describe a pattern of text. The exact (literal) matching we have used so far is also a regular expression. For example, earlier we searched for regular expression terms like "shop" and "delivery".

In PHP, matching regular expressions is more like strstr() matching, rather than equality comparison, because it is at a certain position in a string (if not specified, it may be anywhere in the string) position) matches another string. For example, the string "shop" matches the regular expression "shop". It can also match the regular expressions "h", "ho", etc.

In addition to exact matching characters, special characters can also be used to specify the meta-meaning of the expression. For example, using special characters, you can specify a pattern that must exist at the beginning or end of a string, that part of the pattern may be repeated, or that the characters in the pattern belong to a specific type. Additionally, you can match by occurrence of special characters. Next, we'll discuss these changes one by one.

2 Character sets and classes

Using character sets can immediately provide regular expressions that are more powerful than exact matching. Character sets can be used to match any character of a specific type; in fact, they are a kind of wildcard character.

First of all, you can use a character as a wildcard character to replace any character except the newline character (\n). For example, the regular expression:

.at

can match "cat", "sat", "mat", etc. Typically, this wildcard matching is used for file name matching in operating systems.

However, using regular expressions, you can be more specific about the type of characters you want to match, and you can specify a set to which the characters belong. In the previous example, the regular expression matched "cat" and "mat", but it could also match "#at". If you want to limit it to characters between a and z, you can specify it like this:

[a-z]at

Any content enclosed in square brackets ([]) is a character class - a matched character The character set it belongs to. Note that the expression in square brackets matches only one character.

We can list a set, for example:

[aeiou]

can be used to represent vowels.

can also describe a range, as before with a hyphen, or a range set:

[a-zA-Z]

This range set represents any uppercase or lowercase letter.

In addition, you can also use sets to indicate that characters do not belong to a certain set. For example:

[^a-z]

can be used to match any character that is not between a and z. When the caret (^) is enclosed in square brackets, it means no. When this symbol is used outside square brackets, it means another meaning, which we will introduce in detail later.

3 Repeat

Often, readers will want to indicate that a certain string or character class will appear more than once. You can use two special characters instead in regular expressions. The symbol "*" indicates that this pattern can be repeated 0 or more times, and the symbol " " indicates that this pattern can be repeated 1 or more times. These two symbols should be placed after the expression to be acted upon.

For example:

[[:alnum:]]+

means "at least one alphabetic character".

4 Subexpression

Usually, it is very useful to separate an expression into several subexpressions. For example, it can mean "at least in these strings One needs to be an exact match". This can be done using parentheses, in the same way as in mathematical expressions.

For example:

(very)*large

can match "large", "very large", "very very large", etc.

5 Subexpression count

You can use a numeric expression in curly braces ({}) to specify the number of times content is allowed to be repeated. You can specify an exact number of repetitions ({3} means repeating 3 times), or a range of repetitions ({2, 4} means repeating 2 to 4 times), or an open-bottom repetition range ({2, 4} means repeating 2 to 4 times). } means to repeat it at least twice).

For example:

(very){1,3}

means matching "very", "very very" and "very very very".

6 Positioning at the beginning or end of a string

[a-z] pattern will match any string containing lowercase alphabetic characters. It doesn't matter whether the string has only one character, or contains only one matching character in the entire longer string.

You can also determine whether a specific subexpression appears at the beginning, at the end, or in both positions. This is useful when you want to make sure that only the word you are looking for and no other words appear in a string.

The caret (^) is used at the beginning of the regular expression, indicating that the substring must appear at the beginning of the searched string, and the character "$" is used at the end of the regular expression, indicating the substring Must appear at the end of the string.

For example, the following matches bob at the beginning of the string:

^bob

This pattern will match strings where com appears at the end of the string:

com$

Finally, this The pattern will match a string containing only one character from a to z:

^[a-z]$

7 Branch

可以使用正则表达式中的一条竖线来表示一个选择。例如,如果要匹配com、edu或net,就可以使用如下所示的表达式:

com|edu|net

8 匹配特殊字符

如果要匹配本节前面提到过的特殊字符,例如,.、{或者$,就必须在它们前面加一个反斜杠(\)。如果要匹配一个反斜杠,则必须用两个反斜杠(\\)来表示。

在PHP中,必须将正则表达式模式包括在一个单引号字符串中。使用双引号引用的正则表达式将带来一些不必要的复杂性。PHP还使用反斜杠来转义特殊字符——例如反斜杠。

如果希望在模式中匹配一个反斜杠,必须使用两个反斜杠来表示它是一个反斜杠字符,而不是一个转义字符。

同样,由于相同的原因,如果希望在一个双引号引用的PHP字符串中使用反斜杠字符,必须使用两个反斜杠。这可 能会有些混淆,这样要求的结果将是表示一个包含了反斜杠字符的正则表达式的一个PHP字符串需要4个反斜杠。PHP解释器将这4个反斜杠解释成2个。然 后,由正则表达式解释器解析为一个。

$符号也是双引号引用的PHP字符串和正则表达式的特殊字符。要使一个$字符能够在模式中匹配,必须使用“\\\$”。因为这个字符串被引用在双引号中,PHP解释器将其解析为\$,而正则表达式解释器将其解析成一个$字符。

9 在智能表单中应用

在智能表单应用程序中,正则表达式至少有两种用途。第一种用途是在顾客的反馈中查找特定的名词。使用正则表达 式,可以做得更智能一些。使用一个字符串函数,如果希望匹配"shop"、"customer service"或"retail",就必须做3次不同的搜索。如果使用一个正则表达式,就可以同时匹配所有3个,如下所示:

shop|customer service|retail

第二个用途是验证程序中用户的电子邮件地址,这需要通过用正则表达式来对电子邮件地址的标准格式进行编码。这 个格式中包含一些数字或标点符号,接着是符号“@”,然后是包括文字或数字和字符组成的字符串,后面接着是一个“.”(点号),后面包括文字或数字以连字 符组成的字符串,可能还有更多的点号,直到字符串结束,它的编码如下所示:

^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$

子表达式^[a-zA-Z0-9_\-.]+表示“至少由一个字母、数字、下画线、连字符、点号或者这些字符组合为开始的字符串”。请注意,当在一个字符类的开始或末尾处使用点号时,点号将失去其特殊通配符的意义,只能成为一个点号字符。

符号“@”匹配字符“@”。

而子表达式[a-zA-Z0-9\-]+与包含文字数字字符和连字符的主机名匹配。请注意,我们去除了连字符,因为它是方括号内的特殊字符。

字符组合“\.”匹配“.”字符。我们在字符类外部使用点号,因此必须对其转义,使其能够匹配一个点号字符。

子表达式[a-zA-Z0-9\-\.]+$匹配域名的剩下部分,它包含字母、数字和连字符,如果需要还可包含更多的点号直到字符串的末尾。

不难发现,有时一个无效的电子邮件地址也会符合这个正则表达式。找到所有无效电子邮件几乎是不可能的,但是经 过分析,情形将会有所改善。可以按许多不同的方式精化这个表达式。例如,可以列出所有有效的顶级域(TLD)。当对某些对象进行限制的时候,请千万小心, 因为可能排斥1%的有效数据的校验函数比允许出现10%的无效数据的校验函数还要麻烦。

推荐:《PHP教程

The above is the detailed content of PHP POSIX regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:aliyun. If there is any infringement, please contact admin@php.cn delete
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools