CFC4N test php Regular Expression

Source: Internet
Author: User

Friend A: It is required to reverse the string to a PHP Array Based on a string, and the string provided is printed by print_r of php. For the moment, we will not consider whether this method can ensure data accuracy or other unexpected situations. We will only write regular expressions as required.
The string is
Copy codeThe Code is as follows:
Array
(
[Tt] => Array
(
[Table] => qqttcode
[Hitcode] => 1
)
[Ww] => Array
(
[Table] => qqwwcode
[Hitcode] =>
)
[Pp] => Array
(
[Table] => qqppcode
[Hitcode] => Array
(
[Table] => qqppcode
[Hitcode] =>
)
)
)

CFC4N provides the following results:
Copy codeThe Code is as follows:
$ StrRge1 = '/(\ [([^] +)] \ s? >\S ?)? Array [\ s \ S] +? \ ([^ ()] | (? R) * \)/I ';
$ ArrReturn = array ();
If (preg_match_all ($ strRge1, $ str, $ tt1 ))
{
$ ArrReturn = getarray ($ tt1 [0] [0]);
}
$ ArrReturn2 = array ();
Foreach ($ arrReturn as $ k => $ v)
{
$ ArrReturn2 [$ k] = $ v [$ k];
}
Print_r ($ arrReturn2 );
Function getarray ($ strContents)
{
$ ArrTemp = array ();
$ StrRge = '/\ [([^] +)] \ s? ==>\ S? Array [\ s \ S] +? \ ([^ ()] | (? R) * \)/I ';
$ StrReg2 = '/\ [([^] +?)] \ S? ==>\ S? ([\ D \ w] + )? /';
If (preg_match_all ($ strRge, $ strContents, $ strTemp ))
{
$ Num = count ($ strTemp [1]);
If ($ num> '1 ')
{
For ($ I = 0; $ I <$ num; $ I ++)
{
If (preg_match_all ($ strRge, $ strTemp [0] [$ I], $ arrTTT ))
{
$ ArrTemp [$ strTemp [1] [$ I] = array ();
$ ArrTemp [$ strTemp [1] [$ I] = getarray ($ strTemp [0] [$ I]);
}
Else
{
$ ArrTemp [$ strTemp [1] [$ I] = $ strTemp [0] [$ I];
}
}
}
Else
{
$ ArrTemp [$ strTemp [1] [0] = array ();
$ ArrTemp2 = array ();
If (preg_match_all ($ strReg2, $ strTemp [0] [0], $ straa ))
{
$ Num = count ($ straa [0]);
For ($ I = 0; $ I <$ num-1; $ I ++)
{
$ ArrTemp2 [$ straa [1] [$ I + 1] = $ straa [2] [$ I + 1];
}
}
$ ArrTemp [$ strTemp [1] [0] = $ arrTemp2;
}
}
Return $ arrTemp;
}

The result is usable. However, I found that it can only be used for fixed layer-3 nesting. If N layers are used, this function cannot be used. Later, I modified the Regular Expression and changed it
Copy codeThe Code is as follows:
$ StrRge1 = '/\ [([^] +)] \ s? ==>\ S? Array [\ s] +? \ ([^ ()]) + | (? R) \) +/I ';

However, the problem cannot be solved .. What do you think is my misunderstanding?
Add the First Regular Expression

Regular Expression matching after modification

Friend B: You must batch Add a title to tags in the html string that do not contain the title attribute. The title content is <a href…> Text between </a> ..
CFC4N provides the following answers:
Copy codeThe Code is as follows:
$ Str = '<a> ssss </a> <a href = "ss"> ssss </a> <a title = "ss"> ssss </a> $ Str = preg_replace ('% <((? :(?! Title = "[^"] +? ") [\ S \ S]) +?)> (? :(? <! </A>) [\ s \ S]) +? </A> % im ',' <a title = "\ 2" \ 1> \ 2 </a> ', $ str );
Print_r ($ str );

Copy codeThe Code is as follows:
$ Str = '<a> ssss </a> <a href = "ss"> ssss </a> <a title = "ss"> ssss </a> $ Str = preg_replace ('% <((? :(?! Title = "[^"] +? ") [\ S \ S]) +?)> (? :(? <! </A>) [\ s \ S]) +? </A> % im ',' <a title = "\ 2" \ 1> \ 2 </a> ', $ str );
Print_r ($ str );

As you may see, which of the regular expressions written by CFC4N can be optimized? Is the efficiency low ??

Friend C: It is required to filter the UBB tag links of non-local domain names or other domain names that are not subdomain names. Once included, replace them with the text in the middle. For example, the example string is as follows:
Copy codeThe Code is as follows:
[Url = http://www.sadas.cn] baidu [/url]

[Url = www.ggasdwe.com] Baidu [/url]
[Url = http://www.qq.com/index.php?qq=/url]

[Url = http://www.miyifun.com/index.html?others

[/Url]
[Url = pc.qq.com/index.php?pc QQ [/url]

The string does not contain several line breaks or other characters, and you are not sure whether the url in the url's UBB tag contains http: // or a second-level or third-level domain name.

The Regular Expression and PHP code given by CFC4N are as follows:
Copy codeThe Code is as follows:
$ Str = '[url = http://www.sadas.cn] baidu [/url]

[Url = www.ggasdwe.com] Baidu [/url]
[Url = http://www.qq.com/index.php?qq=/url]

[Url = http://www.miyifun.com/index.html?others

[/Url]
[Url = pc.qq.com/index.php?pc QQ [/url] ';
Print_r (preg_replace ('% \ [url = (http ://)? (? :(?! Qq \. com) [^ \]) * \] [\ r | \ r \ n] * ([\ s \ S] + ?) [\ R | \ r \ n] * \ [/url \] % I ',' \ 2', $ str ));

What do you think is redundant? What other regular expressions can be optimized to improve efficiency? If you do not understand it, where are your questions?

Friend Ding: requires reading the lines that work in the squid configuration file, that is, the lines that do not start #..
The strings in the squid configuration file are listed in the attachment.
Squid Configuration File Content
CFC4N provides the following regular code:
Copy codeThe Code is as follows:
Preg_match_all ('/^ (?! #). +? $/M', file_get_contents ('squid. conf'), $ regs );
Print_r ($ regs [0]);

Run

What else do you think this regular expression is worth noting? Is it correct to match what Ding needs? Do you have any questions?

PS: The above regular expressions are all PCRE engines .. The regular recursion (iteration) Section of PHP Code is applicable only to engine code that supports recursive regular expressions ..
Thank you, Boss rex (?!) The matching rule may be invalid after the non-matching feature of the zero-width assertion.

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.