PHP is not a regular expression _ regular expression containing a string

Source: Internet
Author: User

Common function Strstr ($STR, "abc");
Regular matching Preg_match ("/(ABC)/is", $str);
But to match a string that does not contain a string, it is more troublesome to use the positive
If not regular!strstr ($STR, "abc"); We can solve the problem.
But with the regular, it's the only way, "/^ (?!) ABC).) *$/is "
//------------------------------------------------

Copy Code code as follows:

<?php
$str = "DFADFADF765577ABC55FD";
$pattern _url = "/^ (?! ABC).) *$/is ";
if (Preg_match ($pattern _url, $str)) {
echo "does not contain abc!. ";
}else{
echo "contains abc! ";
}
?>

//------------------------------------------------
The result is: false, containing abc!
$str = "2b3c4d5c";
Note: [^ (ABC)] This syntax is to check whether the characters in $str are not in a B C,
Preg_match ("/[^ (ABC)]/s", $str, $arr);
Where the character 2 is not in a B C, so the $arr return value is 2;
Matches at the same time, contains the string "ABC" and does not contain the string "XYZ"
"/(ABC) [^ (?! XYZ).) *$]/S "

The following are supplementary:

There are many ways to determine whether a string contains another string, as follows:

1. Common functions

Strstr ($STR, "abc");

2. Regular matching

Preg_match ("/(ABC)/is", $str);

But to match a string that doesn't contain a string, it's more troublesome to use the positive.
1. If not, the following can be used to solve the problem

!strstr ($STR, "abc");

2. But with the regular, it's the only way.

Preg_match ("/^" (?!) ABC).) *$/is ", $STR);

Complete code example

Copy Code code as follows:

$str = "DFADFADF765577ABC55FD";
$pattern _url = "/^ (?! ABC).) *$/is ";
if (Preg_match ($pattern _url, $STR))
{
echo "does not contain abc!. ";
}
Else
{
echo "contains abc! ";
}

The result is: false, containing abc!

Matches, contains the string "ABC" and does not contain a regular expression of the string "xyz":

Preg_match ("/(ABC) [^] [(?!] XYZ).) *$]/is ", $STR);

This method is effective, I use the following methods:

(?:(?! <\/div>). | \ n) *? Match a string that does not contain </div>

But the result of the final use is that the method is extremely inefficient, you can consider using a very short text (more than 10 words, or up to dozens of) for the same part of the regular formula to be used, but do not use it for a large section of the article, or if you need to replace it in more than one place, consider replacing it with another method such as: Parsing the text that matches the regular style of the paragraph, and then verifying that there is a paragraph of text in it, the regular expression is not a very efficient way to match text segments that do not contain a specific string.

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.