Skip to content

Instantly share code, notes, and snippets.

@hitenpratap
Last active December 1, 2015 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitenpratap/32c58023a95c6a1c8afa to your computer and use it in GitHub Desktop.
Save hitenpratap/32c58023a95c6a1c8afa to your computer and use it in GitHub Desktop.
Extract particulars tags from String using regex
package com.hprog99;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExtractStrTag {
private static Pattern tagPattern =
Pattern.compile("<imageTag>(.+?)</imageTag>"); //here replace <imageTag> with your required tag
public static List<String> getTagList(String str){
final List<String> tagValues = new ArrayList<String>();
final Matcher matcher = tagPattern.matcher(details);
while (matcher.find()) {
tagValues.add(matcher.group(1));
}
return tagValues;
}
public static void main(String pwds[]){
String rawString = "<p><em><strong>gfbgbtyhmjtu myu mty mut ,uy ,rutm yjnhrt hdtjgher gerugfrhjs gfsjrbgvhfdsgf yergf egbvhrg fgyes</strong></em></p>\n" +
"<p><imageTag>1895409.jpg</imageTag></p>\n" +
"<p><strong>bfrttttttthbulgihdtyhblthigltnliytkmityumiytj ytmjkd yjmrum kk76yj rutkmu,m ryukm,iy, r</strong></p>\n" +
"<p><imageTag>1369106.jpg</imageTag></p>";
System.out.println(getTagList(rawString));
}
}
//Output will be something like this
//["1895409.jpg","1369106.jpg"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment