The pattern does compile, it just does not match the string. You are looking for \S+
, but that does not match the space in the name. Instead, you could, e.g., try "everything except <
", i.e. [^<]+
Pattern p3 = Pattern.compile("<emp_dist_nm>([^<]+)</emp_dist_nm>");
In fact, since you already have the closing tag after the group you want to capture, you could also just use .+?
, i.e. a non-greedy group of any character. Here, the non-greedy ?
is important, otherwise it might merge the contents of two such tags and everything in between.
Pattern p3 = Pattern.compile("<emp_dist_nm>(.+?)</emp_dist_nm>");
If you also want to allow empty tags, use *
instead of +
, i.e. [^<]*
or .*?
.