String Matching (Regular Expression) that does not contain a string)

Source: Internet
Author: User

There are many methods to determine whether a string contains another string:

1. Common functions

Strstr ($ STR, "ABC ");

2. Regular Expression matching

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

However, to match a string that does not contain a specific string, it is more difficult to use regular expressions.

1. Solve the problem without using the following regular expressions:

! Strstr ($ STR, "ABC ");

2. But use regular expressions.

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

CompleteCodeExample

$ STR = "dfadfadf765577abc55fd ";

$ Pattern_url = "/^ ((?! ABC).) * $/is ";

If (preg_match ($ pattern_url, $ Str ))

{

Echo "does not include ABC! ";

}

Else

{

Echo "contains ABC! ";

}

Result: false, containing ABC!

The regular expression that contains the string "ABC" and does not contain the string "XYZ:

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

This method is valid and is used as follows:

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

However, the final result shows that the method is extremely inefficient and can process very short texts (a dozen or dozens of words must match the same part of the regular expression) time can be considered for use, but when used for large spaceArticleThe time needed for resolution or multiple matching changes should not be used, and other methods should be used for replacement (for example, first parse the text that needs to match the regular expression of the paragraph, and then verify whether there is a paragraph in it ), regular expressions are not very effective when matching text segments without specific strings.


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.