Home Backend Development PHP Tutorial PHP Perl风格正则表达式

PHP Perl风格正则表达式

Jun 23, 2016 pm 01:15 PM

1、正则表达式作为一个匹配的模版,是由原子(普通字符,例如a-z),有特殊功能的字符(元字符,例如*、+ 和?等),以及模式修正符三个部分组成。
2、在于Perl兼容的正则表达式函数中使用模式时一定要给模式加上定界符,即将模式包含再两个反斜线  /  之间 。

一、定界符
再使用Perl兼容的正则表达式时,要将模式表达式放进定界符之间。作为定界符不仅仅局限使用"/",除了数字、字符、和反斜线\意外的任何字符都可以作为定界符号。不过通常都习惯讲模式表达式包含在两个斜线/之间。

二、原子
原子是正则表达式的最几本组成单位,再每个模式中最少要包含一个原子。
1、普通字符作为原子
   a~z    、A~Z 、0 ~ 9等

2、一些特殊字符和元字符作为原子
任何一个符号都可以当做原子使用,但是如果这个符号再正则表达式中表示一些特殊涵义。则必须使用转义字符取消特殊涵义,将其变成普通原子。

3、一些非打印字符作为原子 所谓的非打印字符,就是一些在字符串中的格式控制符号。例如空格、回车、以及制表符。
   

4、使用“通用字符类型"作为原子
  不管是打印字符还是非打印字符作为原子都是一个原子只能匹配一个字符。而有事我们需要一个原子可以匹配一类字符。如匹配数字、匹配字母。

5、自定义原子表([])作为原子

使用自定义出特定的“类原子”,使用原子可以定义意足彼此平级的原子。
如“/[jhp]sp/” 可以匹配jsp 、hsp 、psp三种


三、元字符
 利用Perl正则表达式还可以使用各种元字符来搜索匹配。所谓元字符,就是用于构建正则表达式的具有特殊涵义的字符。如 . * ? + 等。在一个正则表达中,元字符不能单独出现,它是用来修饰原子的。如同我们中文的形容词一样,必须有形容的主体才醒。例如:我很帅。帅是形容词,形容的是我。元字符可以做这方面的理解和想象。
    构建正则表达式的方法和写文章的方法一样,就是使用多种形容词将某个人物或者事情清晰直观的表达出来。正则表达式的组件可以是单个的字符、字符集合、字符范围、字符间的选择或者是所有这些组件的任意组合。下面将这些字符分为几个小类,分别阐述。

  
  1、限定符。限定次数。
  2、边界限制

  3、原点,在字符之外,模式中的原点可以匹配目标中的任意一个字符,包括不可打印字符,但不匹配换行符。如果设定了模式修正符s,则也会匹配换行符。

  4、模式选择符(|)匹配选项中的任意一组。

  5、模式单元。使用元字符()将多个原子组成大的原子,被当做一个单元独立使用。

  6、后向引用。

  7、模式匹配的优先级:从左到右。

 

四、模式修正符。

  

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles