Python strip_tags supports retention of specified labels

Source: Internet
Author: User
#coding: Utf-8import redef strip_tags (String, allowed_tags= "):  if Allowed_tags! =":    # Get A list of all allowed Tag names.    Allowed_tags = Allowed_tags.split (', ')    allowed_tags_pattern = [' </? ' +allowed_tag+ ' [^>]*> ' for Allowed_tag in allowed_tags]    all_tags = Re.findall (R ' <[^>]+> ', string, re . I)    not_allowed_tags = []    tmp = 0 for    tag in all_tags: for        pattern in Allowed_tags_pattern:            rs = re.ma TCH (Pattern,tag)            if RS:                tmp + = 1            else:                tmp + = 0        if not tmp:            not_allowed_tags.append (tag )        tmp = 0    for Not_allowed_tag in not_allowed_tags:        string = Re.sub (Re.escape (Not_allowed_tag), ", String)    print not_allowed_tags  else:    # If No allowed tags, remove all.    String = Re.sub (R ' <[^>]*?> ', ', string)   return string

  

Python strip_tags supports retention of specified labels

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.