search
Home类库下载PHP类库[Consolidating the PHP series] PHP regular expressions

【Consolidating PHP series】PHP regular expressions

1. Overview

1. Regular expression test address: http://tool.chinaz.com/regex/

2. Specific commonly used regular expressions

1. Email regular expression:
 1) w[-w.+]*@([A-Za-z0-9][-A-Za-z0-9]+.)+[A-Za- z]{2,14}
 2) w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
 3) Analysis:

 -- " " " is called "escape character". Used to escape some special symbols, such as ".","/"

  -- "w" Equivalent to [a-zA-Z0-9_]. Also matches Chinese characters

   --"+"                                 1 to 1 or more times, can also be written as {1,}

   --"Parentheses" are used to specify subexpressions (also called groupings), and then you can specify the repetition of this subexpression times, ([-+.]w+)*

   --[-+.] The part expanded by square brackets is the character field

   --.

<?php
  $email = &#39;sishuinianhua@qq.com&#39;;
  $preg = &#39;/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/&#39;;
  $res = preg_match($preg, $email, $preg_res);
  var_dump(111,$res,$preg_res);exit;

 --preg_match(): http://php.net/manual/zh/function.preg-match.php

2. URL expression:

 1) [a-zA-z]+:// [^s]+

 2) Analysis:

 -- ^

   > Put it in the character field (such as: [^w]) to express negation (meaning not to include) - "reverse selection"

     Placed before the expression, it means starting with the current character. (/^n/i, means starting with n).

   -- s Matches any whitespace character

3. Appendix

1. Commonly used metacharacters

Code Description

. Matches any character except line breaks

w Matches letters or numbers or underscores
s match Any whitespace character
d Matches numbers
b Matches the beginning or end of a word
^ Matches the beginning of a string
$ Matches the end of a string


2. Commonly used qualifiers

Code/Syntax Description

* Repeat zero times or more times
+   Repeat one or more times
? Repeat zero or once
{n} Repeat n times
{n,} Repeat n times or more
{n,m} Repeat n to m times


3. Commonly used antonyms

Code/Grammar Description

W Matches any character that is not a letter, number, underscore, or Chinese character
S Matches any character that is not a whitespace character
D Matches any character that is not a digit
B Matches something other than the beginning of a word Or the ending position
[^x] Matches any character except x
[^aeiou] Matches any character except the letters aeiou


Statement
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

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment