java - Match the last occurrence of a word in a string -



java - Match the last occurrence of a word in a string -

currently, implementing textbox in java swing, highlights keywords. referenced 2nd reply given in this question. however, want lastly occurrence of word highlighted.

for example, when user typing "i have swimming @ school @ 7pm", want textbox highlight sec "at". have tried using negative lookahead in regex. not working.

i used (at(?!.at)|from|by))) instead of (at|from|by)

what have tried (referenced link provided):

if (wordr == after || string.valueof(text.charat(wordr)).matches("\\w")) { if (text.substring(wordl, wordr).matches("(\\w)*(at(?!.at)|from|by)")) setcharacterattributes(wordl, wordr - wordl, attr, false); else setcharacterattributes(wordl, wordr - wordl, attrblack, false); wordl = wordr; }

i think regex not working checking happening while user typing, not sure on how solve problem.

why don't utilize this method:

public int lastindexof(string str)

returns index within string of lastly occurrence of specified substring, searching backward starting @ specified index.

as in:

system.out.println("index = " + "i have swimming @ school @ 7pm".lastindexof("at"));

you can utilize value returned lastindexof() set attributes of string in swing component, obviously, you'll need reset you've done explained here: resetting attributes in document after inserting string

if don't this, every occurrence of word remain highlighted.

java regex string replace

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -