Use regular expressions to process multiple substrings in a string

Source: Internet
Author: User

Cause

When I worked yesterday, I encountered the following requirement: I marked the keywords in the retrieved string in red, and the number of keywords is not fixed. It is actually quite simple, but I didn't think of a good way at the moment, because it is not simply to replace the target string with a unified string, but the target string must be processed. To be honest, there are not many regular expressions in Java, so regular expressions have always been incomprehensible, But I believe regular expressions can be solved, and it should be very simple.

With such confidence, I checked the JDK API and thought it was quite simple. There were at least one solution.

JAVA Implementation

In fact, the problem lies in how we can obtain the strings matched by regular expressions, and then splice them to the original string. For example, we get the matched string substr and wrap it with the span tag:

 
"<Span style = 'color: red; '>" + substr + "</span>"

So how do we obtain the string matched by the regular expression? This should be related to the regular expression we wrote. because the number of keywords is not fixed, we can use each keyword as an expression to process the original string accordingly, after a keyword match is processed, perform the next keyword operation until the end:

Public String signkeywordtored (string content, string [] keywords) {stringbuffer = new stringbuffer (); For (string Keyword: keywords) {If (! Stringutils. isempty (keyword) {matcher = pattern. compile (keyword, pattern. case_insensitive ). matcher (content); While (matcher. find () {matcher. appendreplacement (stringbuffer, "<span style = 'color: red; '>" + matcher. group (0) + "</span>");} matcher. appendtail (stringbuffer);} content = stringbuffer. tostring (); stringbuffer. delete (0, stringbuffer. length ();} return content ;}

In a regular expression group, group 0 always represents the entire expression, so we can use group (0) to obtain the string matched by the regular expression (that is, the keyword); except group (0) in addition to obtaining the matching string, you can also use the string truncation method to obtain the matching string. For example, you can do this:

 
Matcher. appendreplacement (resultbuffer, "<span style = 'color: red; '>" + content. substring (matcher. start (), matcher. end () + "</span> ");

The START () method and end () method of the matcher object return the start index and end index that match the string respectively, and the pattern. the second parameter pattern in the compile () method. case_insensitive indicates case insensitive.

In addition to the above method, we should be able to think of another way: combine all the keywords into a regular expression and compile the expression only once. For example, we have two keywords man and love, and our regular expression can be written as follows: (man | love). Then, when the number of keywords is not fixed, we can do this:

 
Public String signkeywordtored (string content, string [] keywords) {stringbuffer Regexp = new stringbuffer ("("); For (string Keyword: keywords) {Regexp. append (keyword ). append ("|");} Regexp. deletecharat (Regexp. length ()-1 ). append (")"); matcher = pattern. compile (Regexp. tostring (), pattern. case_insensitive ). matcher (content); stringbuffer resultbuffer = new stringbuffer (); While (matcher. find () {matcher. appendreplacement (resultbuffer, "<span style = 'color: red; '>" + matcher. group (1) + "</span>");} matcher. appendtail (resultbuffer); Return resultbuffer. tostring ();}

In this example, the above group (1) can be changed to group (0.

Implementation in Javascript

After implementing the requirements, it should be easier to put this problem in JS, because JavaScript flexibility is hard to match with Java, a more rigorous language. So I started to try it. It was very simple. I only needed a few lines.CodeAs shown in the following figure:

 
Function signkeywordtored (content, keywords) {var Regexp = new Regexp ("(" + keywords. join ("|") + ")", 'gi'); Return content. replace (Regexp, "<span style = 'color: red; '> $1 </span> ");}

Here, $1 shares the same principle as group (1) in Java, which is the string matched by the first group of the regular expression. Note that here we assume that the parameter Keywords of the input method is an array. Here is only a small example. If you need more rigorous processing, you need to determine whether it is an array.

Below is a small example:

<HTML>  

The Replace () method of the string object in Javascript is a very powerful method and needs to be carefully studied.

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.