java - Extract substring (that contains a dynamic portion) from a string? -


say have following sub-string resides inside larger string:

my name {name}

{name} placeholder because might different every time program run. example:

run #1: hi there, name phil! nice meet you.

run #2: name robert, , really, cool.

how can (in concise way possible):

  1. verify string contains my name {name} sub-string? (case insensitive)
  2. extract {name} part separate variable can with?

you can this:

string s = "hi there! name phil. nice meet you."; pattern p = pattern.compile("my name (\\w*)", pattern.case_insensitive); matcher m = p.matcher(s); if (m.find()) {     system.out.println(m.group(1)); } else {     system.out.println("no match " + s); } 

the (\\w*) creates capture group of "word characters". more information can found @ docs pattern , matcher.

p.s. it's been hard track requirements through edits. hope matches need. if not, should straightforward modify it.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -