Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 正则表达式字符串取反 怎么弄?

正则表达式字符串取反 怎么弄?

Jun 23, 2016 pm 02:24 PM

正则表达式 正则 url

我有一个url规则如下:

mm.[*|a|b|add].cn

其中 a , b , add 表示例外

例如

 mm.a.cn 无法匹配

 mm.x.cn 可以匹配

如何写正则???

我只知道 mm\.[a|b|add]\.cn 可以匹配所有的例外,但是我就是想要一个取反的结果?

回复讨论(解决方案)

$s1= 'mm.a.cn';$s2= 'mm.add.cn';$s3= 'mm.c.cn';$p='/mm\.(.+)(?<!a|b|add)\.cn/';$bool1=preg_match($p,$s1);$bool2=preg_match($p,$s2);$bool3=preg_match($p,$s3);var_dump($bool1,$bool2,$bool3);
Copy after login
Copy after login

int(0) int(0) int(1)

preg_match('#www\.(a|b|add)\.cn#',$str)
Copy after login


要不匹配的话

!preg_match('#www\.(a|b|add)\.cn#',$str)
Copy after login

只能对LZ说??这个正则很难写的

$s1= 'mm.a.cn';$s2= 'mm.add.cn';$s3= 'mm.c.cn';$p='/mm\.(.+)(?<!a|b|add)\.cn/';$bool1=preg_match($p,$s1);$bool2=preg_match($p,$s2);$bool3=preg_match($p,$s3);var_dump($bool1,$bool2,$bool3);
Copy after login
Copy after login

int(0) int(0) int(1)
试试 mm.aadd.cn ?

$s =<<< TXTmm.a.cn mm.x.cn mm.aadd.cnmm.c.cnTXT;preg_match_all('/mm\.(?!a|b|add).*?\.cn/s', $s, $r);print_r($r);preg_match_all('/mm\.(?!a\.|b\.|add\.).*?\.cn/s', $s, $r);print_r($r);
Copy after login
Array
(
    [0] => Array
        (
            [0] => mm.x.cn
            [1] => mm.c.cn
        )

)
Array
(
    [0] => Array
        (
            [0] => mm.x.cn
            [1] => mm.aadd.cn
            [2] => mm.c.cn
        )

)

preg_match_all('/mm\.(?!a\.|b\.|add\.).*?\.cn/s', $s, $r);
可写作
preg_match_all('/mm\.(?!(?:a|b|add)\.).*?\.cn/s', $s, $r);

preg_match_all('/mm\.(?!a\.|b\.|add\.).*?\.cn/s', $s, $r);
可写作
preg_match_all('/mm\.(?!(?:a|b|add)\.).*?\.cn/s', $s, $r);
把后一个字符也加进去,整体作为排除对象啊?受教了

四楼应该可以啊,先结贴了。

无意中看到这个贴的。。
首先。。我不懂PHP,作为正则的角度来说,这个不是很简单么。。
反义排除掉这几个字符就行了:
^mm\.[^ab(?:add)]+\.cn$
[^ab(?:add)] --> 排除a b add

preg_match_all('/mm\.(?!a\.|b\.|add\.).*?\.cn/s', $s, $r);
可写作
preg_match_all('/mm\.(?!(?:a|b|add)\.).*?\.cn/s', $s, $r);

版主这个写法是不是有点多余~

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 Article Tags

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles