C # Replace the regular expression with a string that ignores case sensitivity,

Source: Internet
Author: User

C # Replace the regular expression with a string that ignores case sensitivity,

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 Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

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.