Java USES regular expressions to intercept the contents between specified strings:
package com.accord.util;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式匹配两个字符串之间的内容
* @author Administrator
*
*/
public class RegexUtil {
public static void main(String[] args) {
String str = "<?xml version='1.0' encoding='UTF-8'?><ufinterface billtype='gl' filename='e:\1.xml' isexchange='Y' proc='add' receiver='1060337@1060337-003' replace='Y' roottag='sendresult' sender='01' successful='Y'><sendresult><billpk></billpk><bdocid>w764</bdocid><filename>e:\1.xml</filename><resultcode>1</resultcode><resultdescription>单据w764开始处理...单据w764处理完毕!</resultdescription><content>2017.09-记账凭证-1</content></sendresult><sendresult><billpk></billpk><bdocid>w1007</bdocid><filename>e:\1.xml</filename><resultcode>1</resultcode><resultdescription>单据w1007开始处理...单据w1007处理完毕!</resultdescription><content>2017.10-记账凭证-1</content></sendresult><sendresult><billpk></billpk><bdocid>w516</bdocid><filename>e:\1.xml</filename><resultcode>1</resultcode><resultdescription>单据w516开始处理...单据w516处理完毕!</resultdescription><content>2017.07-记账凭证-50</content></sendresult></ufinterface>";
//String str = "abc3443abcfgjhgabcgfjabc";
String rgex = "<bdocid>(.*?)</bdocid>";
System.out.println((new RegexUtil()).getSubUtil(str,rgex));
List<String> lists = (new RegexUtil()).getSubUtil(str,rgex);
for (String string : lists) {
System.out.println(string);
}
System.out.println((new RegexUtil()).getSubUtilSimple(str, rgex));
}
/**
* 正则表达式匹配两个指定字符串中间的内容
* @param soap
* @return
*/
public List<String> getSubUtil(String soap,String rgex){
List<String> list = new ArrayList<String>();
Pattern pattern = Pattern.compile(rgex);// 匹配的模式
Matcher m = pattern.matcher(soap);
while (m.find()) {
int i = 1;
list.add(m.group(i));
i++;
}
return list;
}
/**
* 返回单个字符串,若匹配到多个的话就返回第一个,方法与getSubUtil一样
* @param soap
* @param rgex
* @return
*/
public String getSubUtilSimple(String soap,String rgex){
Pattern pattern = Pattern.compile(rgex);// 匹配的模式
Matcher m = pattern.matcher(soap);
while(m.find()){
return m.group(1);
}
return "";
}
}
operation results:
[w764, w1007, w516]
w764
w1007
w516
w764
Read More:
- Regular expressions filter special characters
- Java regular matching returns all matches
- Regular error in UDF java.lang.stackoverflowerror
- How to Split numbers and strings in a strings
- 43. Implementation of the shortest code of multiply strings | Java
- How to replace strings in VIM
- hypercan Have unordered match in sub-expressions.
- El expressions are not parsed and output as is
- Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- Reverse function: reverses container contents
- Java – how to shuffle an ArrayList
- PHP function file_ get_ Contents() reports an error when using HTTPS protocol: SSL operation failed
- The node rimraf module recursively deletes the contents of the folder
- The conversion between [Python] bytes and hex strings.
- Java – read all the files and folders in a certain directory and three methods to get the file name from the file path
- Linux remote copy command and not a regular file solution
- Regular view interface consumes more than 4 digits
- SCP error: not a regular file
- ./src/App.js Line 13:11: Parsing error: Unterminated JSX contents (react)
- Python: crawler handles strings of XML and HTML