One day, a great person asked me to mark up
thekeyword, regardless of the size and
do not change the keyword of the search source`, and see it. Please accept it.
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
.
keyword
before(? I)
regardless of size (i stands for incentive)keyword
in()
to make one group$ 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);
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~
}
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.
http://a4dosanddos.hatenablog.com/entry/2016/04/06/002657
Recommended Posts