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):
- verify string contains
my name {name}sub-string? (case insensitive) - 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
Post a Comment