/**
* @author Hill
* @date 2018-03-12
*/
public class SelectElementByTags {
public static void main(String[] args) {
String str = "test <%wanqiang%>test<%24%>teste<%M%>teststests";
getParam(str).forEach((k,v)->{
System.out.println(k+"--->" + v);
});
}
public static Map<String, String> getParam(String str) {
StringBuilder sb = new StringBuilder(str);
Map<String, String> map = new HashMap<String, String>();
while(str.contains("%>")) {
int index_begin = sb.indexOf("<%");
int index_end = sb.indexOf("%>");
if(index_begin == -1 || index_end == -1) {
return map;
} else {
map.put(index_begin + "", sb.substring(index_begin+2, index_end).toString());
sb.replace(index_begin, index_end+2, "");
}
}
return map ;
}
}
/*** 正则表达式简便方法*****/
public class RegxTest {
public static void main(String[] args) {
String str = "test<%one%>test<%two%>test<%three%>test<%four%>";
String regx_text = "<%.*?%>";
Pattern compile = Pattern.compile(regx_text);
Matcher matcher = compile.matcher(str);
while(matcher.find()) {
System.out.println(matcher.group());
}
}
}
字符串中根据标签获取元素
最新推荐文章于 2021-08-15 14:16:44 发布