A regular expression-php Tutorial

Source: Internet
Author: User
A regular expression problem
$string='dj%a3%a3999';if(preg_match('/^[dj]{2}([(\%23)(\%a3\%a3)])((\w)+)$/i', $string,$result)){ print_r($result);}

There is no matching solution.


Reply to discussion (solution)

You 'd better talk about your requirements. The middle section shows that your regular expressions are messy.

/* The requirement is that one dj group % a3 % a3 or % 23 is another group. 999 is a group that obtains the corresponding data through $ result. For example: */$ result [0] = 'DJ'; $ result [1] = '% a3 % a3'; $ result [2] = '2016 ';

$s = 'dj%a3%a3999';preg_match('/([^%]+)((?:%a3|23)+)(.+)/', $s, $r);print_r($r);
Array ([0] => dj % a3 % a3999 [1] => dj [2] => % a3 % a3 [3] => 999)

This is used:

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


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

$s = 'dj%a3%a3999';preg_match('/([^%]+)((?:%a3|23)+)(.+)/', $s, $r);print_r($r);
Array ([0] => dj % a3 % a3999 [1] => dj [2] => % a3 % a3 [3] => 999)

If $ s = 'DJ % 100'; then no.

This is used:

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

[/Code]
This string also needs to match: $ string = 'DJ % 100 ';

$ S = 'DJ % 8080 ';
Preg_match ('/([^ %] + )((? : % (? : A3 | 23) +) (. +)/', $ s, $ r );
Or
Preg_match ('/([^ %] + )((? : % A3 | % 23) +) (. +)/', $ s, $ r );

Regular expression rules are described as follows:
| If ambiguity occurs during calculation, you must list them one by one or enclose them in parentheses.

Apparently (? % A3 | 23). % is combined with a3, causing ambiguity.

Boss, can you help me explain that I see many regular expressions added in front of them? : I want to know what this means?

(? :...) Is the same as (...) grouping, but the result does not show a result set.

(? :...) Is the same as (...) grouping, but the result does not show a result set.

Boss, can you match an address for me? for example:

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

In this way, a maximum of nine URLs can be connected, including sohu.com, baidu.com, and yahoo.com.cn.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.