Replace multiple sets of text with regular expressions

Source: Internet
Author: User

How do I use regular expressions to clean up a few of the sensitive words that appear in an article, and use underscores to connect the sensitive words that appear?

There may be multiple groups of sensitive words, and the number of occurrences of each group is random.

Like an article like this

Blog Park has a lot of good blog posts.

Suppose sensitive words Blog park, blog.

After processing

Bo _ guest _ garden has a lot of good bo _ Wen.

Regular expressions provide a convenient way for us.

Regex.Replace (Input, Rules,new matchevaluator (delegate (Match m) {   if (string. IsNullOrEmpty (m.value) | |     M.value.tochararray (). Length = = 0) return string. Empty;   return string. Join ("_", M.value.tochararray ());}));

The first parameter is the retrieved article.

The second parameter is a matching rule that supports multiple lookups, with the intermediate use of |. such as Blog Park | articles.

The third is a custom method that handles matching to the data.

In the example above, rules: Blog Park | articles

Delegate will execute two times. The first match to the blog Park, the second is the article.

Replace multiple sets of text with regular expressions

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.