Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 一个正则的问题

一个正则的问题

Jun 23, 2016 pm 02:39 PM

1

$string='dj%a3%a3999';if(preg_match('/^[dj]{2}([(\%23)(\%a3\%a3)])((\w)+)$/i', $string,$result)){ print_r($result);}

Copy after login

匹配不上,有什么好的解决方案。


回复讨论(解决方案)

你最好讲一下你的需求吧,中间那段看你的正则很乱。

1

/*需求就是dj一个分组%a3%a3 或者%23 是另外一个分组999是一个分组通过$result获取对应的数据。如:*/$result[0]='dj';$result[1]='%a3%a3';$result[2]='999';

Copy after login

1

$s = 'dj%a3%a3999';preg_match('/([^%]+)((?:%a3|23)+)(.+)/', $s, $r);print_r($r);

Copy after login
Copy after login
Array ( [0] => dj%a3%a3999 [1] => dj [2] => %a3%a3 [3] => 999 )

使用了这个:

1

$string='dj%a3%a3999';if(preg_match('/^([dj]{2})([%a3]+)(\d{3})$/i', $string,$result)){ print_r($result);}

Copy after login
Copy after login


结果:

1

Array ( [0] => dj%a3%a3999 [1] => dj [2] => %a3%a3 [3] => 999 )

Copy after login

1

$s = 'dj%a3%a3999';preg_match('/([^%]+)((?:%a3|23)+)(.+)/', $s, $r);print_r($r);

Copy after login
Copy after login
Array ( [0] => dj%a3%a3999 [1] => dj [2] => %a3%a3 [3] => 999 )

如果$s = 'dj%23999';则不行。

使用了这个:

1

$string='dj%a3%a3999';if(preg_match('/^([dj]{2})([%a3]+)(\d{3})$/i', $string,$result)){ print_r($result);}

Copy after login
Copy after login

[/code]
这个串也需要匹配的:$string='dj%23999';

$s = 'dj%23999';
preg_match('/([^%]+)((?:%(?:a3|23))+)(.+)/', $s, $r);

preg_match('/([^%]+)((?:%a3|%23)+)(.+)/', $s, $r);

正则表达式规则说明中有:
| 运算时如果发生歧义,则需要逐个开列或用括号括起来

显然 (?:%a3|23) 中 % 被结合到了 a3 从而发生了歧义

老大,能不能帮我解释下,我看到有很多的正则前面都加?:我想知道这个有什么意义?

(?:...) 与 (...) 分组作用一样,但结果不出现结果集中

(?:...) 与 (...) 分组作用一样,但结果不出现结果集中

老大,能不能帮我匹配个地址,如:

www.sohu.com,www.baidu.com,www.yahoo.com.n

这样连着的网址最多为9个,其中也可以匹配sohu.com,baidu.com,yahoo.com.cn

(?!(w))(\w+(-\w+)*)(\.(\w+(-\w+)*))*

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)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

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

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

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

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

See all articles