Have string strng = "<title>text1</title><title>text2</title>";
How to get array like
arr[0] = "text1";
arr[1] = "text2";
I try to use this, but in result have, and not array text1</title><title>text2
Pattern pattern = Pattern.compile("<title>(.*)</title>");
Matcher matcher = pattern.matcher(strng);
matcher.matches();
While I agree that using an XML / HTML parser is a better alternative in general, your scenario is simple to solve with regex:
List<String> titles = new ArrayList<String>();
Matcher matcher = Pattern.compile("<title>(.*?)</title>").matcher(strng);
while(matcher.find()){
titles.add(matcher.group(1));
}
Note the non-greedy operator .*? and use of matcher.find() instead of matcher.matches().
Reference:
Pattern > Reluctant QuantifiersMatcher.find()If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With