C # Replace uppercase and lowercase strings with regular expressions

Source: Internet
Author: User

In C #, it is difficult to replace strings with case-insensitive characters. Even if it takes a lot of effort to replace strings, the efficiency is still very low, the correct method should be to use a regular expression.

To use a regular expression, you must first reference the namespace:

The following is a reference clip:

Usingsystem. Text. regularexpressions;

Then, it is very simple to use:

The following is a reference clip:

RegEx. Replace (string, substring to be replaced, replace the substring character, regexoptions. ignorecase)

The final parameter regexoptions. ignorecase indicates that the case sensitivity is ignored.

However, I want to highlight all the matched substrings in a group of strings (that is, make the font style different from other parts of the string ), the following statement inserts an HTML tag at both ends of the string to highlight the string, but the highlighted string is the search string for the substring. The case is different from the original one.

For example, the keystring I searched in "13th Asp.net implementations" is "asp", and the replaced string is "13th Asp.net implementations ", instead of "13th Asp.net implementations"

The following is a reference clip:

Documentresume [I] = RegEx. Replace (hitdoc. Get ("resume"), keystring, "" + keystring + "", regexoptions. ignorecase );

Therefore, replace directly using a regular expression cannot meet my needs. Instead, we need to use the match search method of the regular expression (match searches for a single entry and multiple matchs entries ), insert the HTML tag before and after the matched substring. For details, refer to the following code:

The following is a reference clip:

Stringpain = hitdoc. Get ("resume"); // string

System. Text. regularexpressions. matchcollectionm = RegEx. Matches (pain, keystring, regexoptions. ignorecase); // ignore keywords in case-insensitive search strings

For (intj = 0; j

{

// J × 31 adds the length of the pain string for inserting HTML tags. Note that the following two sentences cannot be exchanged. Otherwise, the HTML tag insertion error occurs.

Pain = pain. insert (M [J]. index + keystring. Length + J * 31), ""); // Insert the HTML Tag after the keyword

Pain = pain. insert (M [J]. index + J * 31), ""); // Insert the HTML tag before the keyword

}

Of course, HTML tags do not work if they are inserted randomly. Add the following custom style to the header area of the page code for displaying the inserted string.

The following is a reference clip:

<Styletype = text/CSS>

<! --. Highlight {

Color: #00 FFFF;

Font-style: italic;

Font-size: larger;

} -->

</Style>

[Nextpage] nutian: however, in the regular expression mode, case-insensitive replacement should not be the most efficient, but not the lowest. Generally, the application should be able.

Of course, HTML tags do not work if they are inserted randomly. Add the following custom style to the header area of the page code for displaying the inserted string.

The following is a reference clip:

<Styletype = text/CSS>

<! --. Highlight {

Color: #00 FFFF;

Font-style: italic;

Font-size: larger;

} -->

</Style>

Nutian.

C # Replace uppercase and lowercase strings with regular expressions

Related Article

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.