java - Highlight a word based on search pattern in a string content -


i trying write sample java program using regular expression highlights word in string content based on given pattern. tried following code. no luck

pattern pattern = pattern.compile(".*(test).*", pattern.case_insensitive); string replaceall = pattern.matcher(str).replaceall("<span>$1</span>"); 

ex 1:

input content : testing program input pattern : test* expected : <span>testing<span> program 

ex 2 :

input content : testing program input pattern : test expected : <span>test<span>ing program 

thanks in advance.

try this.

    string input = "i testing program";     system.out.println(input.replaceall("(?i)test\\s*", "<span>$0</span>"));     // -> <span>testing</span> program     system.out.println(input.replaceall("(?i)test", "<span>$0</span>"));     // -> <span>test</span>ing program 

(?i) means case_insensitive.


Comments

Popular posts from this blog

ios - UITEXTFIELD InputView Uipicker not working in swift -

Hatching array of circles in AutoCAD using c# -