How to mark up search keywords regardless of case and without changing the search source word

One day, a great person asked me to mark up thekeyword, regardless of the size anddo not change the keyword of the search source`, and see it. Please accept it.

Purpose

Suppose that data contains the text of the search source. On the other hand, the purpose of this time is to return the character string as if it was marked up regardless of the size when searching with keyword.

result

  1. Judge the keyword before(? I)regardless of size (i stands for incentive)
  2. Enclose the keyword in()to make one group
  3. Add the part to be changed + $ 1 (By the way, if there are two groups, $ 1 $ 2)

↓ ↓ It looks like this when written in code ↓ ↓

String keyword = "java";
String data = "Java java javaJava";

// (?i)Is not case sensitive
//Hit characters()Group by
data = data.replaceAll("((?i)" + Pattern.quote(keyword) + ")", "<mark>$1</mark>");

// <mark>Java</mark> <mark>java</mark> <mark>java</mark><mark>Java</mark>
System.out.println(data);

Gist

To the result

I was told that it would be awkward if I did it with a gorigori. .. .. Lol (I was wondering if it was while)

Bad example


Pattern p = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE).matcher(data);
while(p.find()) {
  int start = p.start();
  int end   = p.end();
  ~Calculate from the found location information and fill in the gorigori tag~
}

Impressions

I think I was able to implement it quite neatly and completely. I want to search for anything and mark up and display the results! How to write that is perfect for processing. This is just an example, so please use it as a reference.

Reference site

http://a4dosanddos.hatenablog.com/entry/2016/04/06/002657

Recommended Posts

How to mark up search keywords regardless of case and without changing the search source word
[Rails] How to get the URL of the transition source and redirect
[Java improvement case] How to reach the limit of self-study and beyond
[Java] How to get the URL of the transition source
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
How to add elements without specifying the length of the array
How to change the contents of the jar file without decompressing
How to check the extension and size of uploaded files
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
[Swift5] How to get an array and the complement of arrays
How to set the IP address and host name of CentOS8
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
[Ruby On Rails] How to search the contents of params using include?
How to determine the number of parallels
How to set up and use kapt
How to sort the List of SelectItem
How to find the tens and ones
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
How to find the cause of the Ruby error
Customize how to divide the contents of Recyclerview
Output of how to use the slice method
How to set up and operate jEnv (Mac)
How to display the result of form input
How to find the total score and average score
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
How to set the retry limit of sidekiq and notify dead queues with slack
[Swift5] How to round off, round down, and round up to the second or third decimal place