这是一段示例文本:
我想要提取以下内容:
我错在哪了?
原始代码:
\1f\1e\1d\020028
。我无法修改输入文本,我正在从文件中读取长字符串。
我想要提取以下内容:
\1f
、\1e
、\1d
、\02
为此,我编写了以下正则表达式模式:"\\[a-fA-F0-9]"
我正在使用Pattern
和Matcher
类,但我的匹配器无法使用上述正则表达式找到模式。我已在一些在线正则表达式网站上测试了这个正则表达式,并出奇地得到了结果。我错在哪了?
原始代码:
public static void main(String[] args) {
String inputText = "\1f\1e\1d\02002868BF03030000000000000000S023\1f\1e\1d\03\0d";
inputText = inputText.replace("\\", "\\\\");
String regex = "\\\\[a-fA-F0-9]{2}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(inputText);
while (m.find()) {
System.out.println(m.group());
}
}
输出:没有任何东西被打印出来
\\[a-fA-F0-9]
查找反斜杠后跟一个字母或数字。我认为你想查找反斜杠后跟两个字母或数字。我猜你可以想办法解决这个问题。 - ajb