Java Regex is including new line in match -


i'm trying match regular expression textbook definitions website. definition has word new line followed definition. example:

zither  definition: instrument of music used in austria , germany has thirty forty wires strung across shallow sounding board lies horizontally on table before performer uses both hands in playing on not confounded old lute shaped cittern or cithern 

in attempts word (in case "zither") keep getting newline character.

i tried both ^(\w+)\s , ^(\s+)\s without luck. thought maybe ^(\s+)$ work, doesn't seem match word @ all. i've been testing rubular, http://rubular.com/r/lpehcns0ri; seems match attempts way want, despite fact java doesn't.

here's snippet

string str = ...; //here string assigned word , definition taken internet given in example above. pattern rgx = pattern.compile("^(\\s+)$"); matcher mtch = rgx.matcher(str); if (mtch.find()) {     string result = mtch.group();     terms.add(new searchterm(result, system.nanotime())); } 

this solved triming resulting string, seems should unnecessary if i'm using regular expression.

all appreciated. in advance!

try using pattern.multiline option

pattern rgx = pattern.compile("^(\\s+)$", pattern.multiline); 

this causes regex recognise line delimiters in string, otherwise ^ , $ match start , end of string.

although makes no difference pattern, matcher.group() method returns entire match, whereas matcher.group(int) method returns match of particular capture group (...) based on number specify. pattern specifies 1 capture group want captured. if you'd included \s in pattern wrote tried, matcher.group() have included whitespace in return value.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -