regex - Java regular expression to get a portion of a string -


i want portion of string contains 1 of targeted words. example, following example string:

... def a: ... target1 ... def b: ... def c: ... 

i want part:

def a: ... target1 ... 

here java code:

s = "(def\\w(.*)\\w(target1|target2|target3)\\w(.*)def\\w)"; pattern p = pattern.compile(s); matcher m = p.matcher(sourcestring);  while(m.find()){     system.out.println(m.group(0)); } 

the problem not find out anything.

thanks help!

you can use:

pattern p = pattern.compile(   "(\\bdef\\s((?!\\bdef\\b).)*?\\b(?:target1|target2|target3)\\b.*?(?=sdef))",   pattern.dotall); 

regex demo


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -