Python3 regular match [^ABC] and (?! ABC) differences (exclude multiple characters as a whole match)

Source: Internet
Author: User

Purpose: To find a string that is not ABC after the number

If 1AB meets the requirements, 2ABC does not meet the requirements

1str ='1ab'2out = Re.match (r'\d+ (?! ABC)', str)3 4STR1 ='1ABC'5OUT1 = Re.match (r'\d+ (?! ABC)', str1)6 7 Print('Out :', out)8 Print('OUT1:', OUT1)9 #Ten #Out : <_sre. Sre_match object; span= (0, 1), match= ' 1 ' > One #Out1:none A #

If you put (?! ABC) instead of [^ABC], the effect is as follows:

1str ='1ab'2OUT3 = Re.match (r'\D+[^ABC]', str)3 4STR1 ='1ABC'5Out4 = Re.match (r'\D+[^ABC]', str1)6 7 Print('Out :', OUT3)8 Print('OUT1:', Out4)9 Ten # One #Out3:none A #Out4:none

Python3 regular match [^ABC] and (?! ABC) differences (exclude multiple characters as a whole match)

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.