java - Multiline REGEX - match only the first line, ignore the rest of lines -


here method remote smtp server name:

public static string getmtaname(string data) {      pattern p = pattern.compile("^\\d{3}[ -](.*?)( .*)*$");     matcher m = p.matcher(data);     if (m.find()) {         return m.group(1);     }      return "undefined"; } 

the problem if pass multiline response like:

string s = "220-xsistema.lt esmtpsa xmailserver 1.2 service ready\r\n220 other info"; system.out.println(getmtaname(s)); 

the output "undefined". if:

s = "220-xsistema.lt esmtpsa xmailserver 1.2 service ready"; 

then works fine - output "xsistema.lt". question - how match first line?

. default not match newline.so use [\s\s] instead of . or use dotall flag,

pattern.dotall or (?s) tells java allow dot match newline characters, too.

pattern regex = pattern.compile("^\\d{3}[ -](.*?)( .*)*$", pattern.dotall);


Popular posts from this blog