CFC4N Small test PHP Regular expression _ regular expression

Source: Internet
Author: User
Friend A: The request is based on a string of strings, inverted into a PHP array, which gives the string to the PHP Print_r printed out. We do not consider whether this method can ensure the accuracy of the data, as well as other contingencies, only according to the request to write regular.
Its string is
Copy Code code as follows:

Array
(
[TT] => Array
(
[Table] => Qqttcode
[Hitcode] => 1
)
[WW] => Array
(
[Table] => Qqwwcode
[Hitcode] =>
)
[PP] => Array
(
[Table] => Qqppcode
[Hitcode] => Array
(
[Table] => Qqppcode
[Hitcode] =>
)
)
)

CFC4N Give the result:
Copy Code code as follows:

$strRge 1 = '/(\[(^]]+)]\s?=>\s?)? Array[\s\s]+?\ ([^ ()]| (?) R) *\)/I ';
$arrReturn = Array ();
if (Preg_match_all ($strRge 1, $str, $TT 1))
{
$arrReturn = GetArray ($tt 1[0][0]);
}
$arrReturn 2 = array ();
foreach ($arrReturn as $k => $v)
{
$arrReturn 2[$k] = $v [$k];
}
Print_r ($arrReturn 2);
function GetArray ($strContents)
{
$arrTemp = Array ();
$strRge = '/\[([^]]+)]\s?=>\s? Array[\s\s]+?\ ([^ ()]| (?) R) *\)/I ';
$strReg 2 = '/\[([^]]+?)] \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 ();
$arrTemp 2 = array ();
if (Preg_match_all ($strReg 2, $strTemp [0][0], $straa))
{
$num = count ($straa [0]);
For ($i =0 $i < $num-1; $i + +)
{
$arrTemp 2[$straa [1][$i +1]] = $straa [2][$i +1];
}
}
$arrTemp [$strTemp [1][0]] = $arrTemp 2;
}
}
return $arrTemp;
}

The results are available. But found that it can only be used for fixed three-layer nesting, if n-layer, can not use this function, and later, I changed the regular, to
Copy Code code as follows:

$strRge 1 = '/\[([^]]+)]\s?=>\s? Array[\s]+?\ ([^ ()]) +| (? R)) \ +/i ';

However, it does not solve the problem. You reader, what do you think is wrong with me?
With the first regular screenshot

Regular match screenshot after change

Friend B: Require batch to add title to the label in the HTML string that does not contain the title attribute, and the title content is the text between <a href...> to </a>.
Cfc4n the answer is:
Copy Code code as follows:

$str = ' <a >ssss</a><a href= "ss" >ssss</a><a title= "ss" >ssss</a><a href= "" > Ssss</a><a title= "ss" >ssss</a><a title= "ss" >ssss</a><a title= "SSF" >ssss</a > ';
$str = preg_replace ('%<a:(?! title= "[^"]+? ") [\s\s]) +?) > (?:(? <!</a>) [\s\s]) +?</a>%im ', ' <a title= ' \\2 ' \\1>\\2</a> ', $str);
Print_r ($STR);

Copy Code code as follows:

$str = ' <a >ssss</a><a href= "ss" >ssss</a><a title= "ss" >ssss</a><a href= "" > Ssss</a><a title= "ss" >ssss</a><a title= "ss" >ssss</a><a title= "SSF" >ssss</a > ';
$str = preg_replace ('%<a:(?! title= "[^"]+? ") [\s\s]) +?) > (?:(? <!</a>) [\s\s]) +?</a>%im ', ' <a title= ' \\2 ' \\1>\\2</a> ', $str);
Print_r ($STR);

You reader, what do you think can be optimized in the regular expression that cfc4n writes? Is this efficiency not high??

Friend C: Request to filter non-this domain name, or not the name of the other domain name of the UBB tag link, once included, directly replace the middle of the text, such as the example string below
Copy Code code 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] Other

[/URL]
[url=pc.qq.com/index.php] PC Qq[/url]

Among them, the string is not certain that there are several newline and other characters, and, the URL is not sure UBB tag in the URL contains http://, not certain level two domain name or level three domain name

The regular and PHP code given by cfc4n is as follows
Copy Code code 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] Other

[/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));

Reader, what do you think is superfluous here? What other regular optimizations can be made to improve efficiency? If you don't understand it, where is your question?

friend Ding: required to read squid in the configuration file, the line that works, that is, there is no # at the beginning of the comment line .
Among them, squid's configuration file within the string is shown in the attachment
Squid's configuration file contents
CFC4N gives the regular code as follows

Copy Code code as follows:

Preg_match_all ('/^ (?!) #). +?$/m ', file_get_contents (' squid.conf '), $regs);
Print_r ($regs [0]);

Run screenshots

Reader, what do you think is the point that is not noticed? Can you correctly match the content of your friend Ding? Do you have any questions?

PS: The above is the Pcre engine. Among them, the regular recursive (iterative) part of the PHP code is limited to the application of the engine code that supports recursive regularization.
Thanks to Rex boss for pointing out (?!) A 0-wide assertion that a mismatched attribute is followed by a problem where the matching rule may not be valid.

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.