我的文件内容是java中的字符串。我需要捕获属性代码的值,即 key.test.text
和key.test.text1
<input type="button" value="<s:message code="key.test.text" />"
<input type="button2" value='<s:message code="key.test.text1' />"
=
之前可以有空格喜欢 <input type="button" value = "<s:message code="key.test.text" />"
我不确定如何使用正则表达式或字符串捕获它?
最佳答案
使用正则表达式模式
String regex = "value\\s*=\\s*[\"']<s:message\\s+code\\s*=\\s*[\"']([^\"']+)[\"']\\s*\\/>";
捕获组 #1 将为每个匹配返回所需的字符串。
<小时/>Java代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String input = "...";
String regex = "value\\s*=\\s*[\"']<s:message\\s+code\\s*=\\s*[\"']([^\"']+)[\"']\\s*\\/>";
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile(regex).matcher(input);
while (m.find()) {
allMatches.add(m.group(1));
}
System.out.println(allMatches);
测试此演示代码 here .
关于java - 捕获字符串中的属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50421967/