在这种情况下,有没有办法用正则表达式找到特殊的子字符串?
Is there a way to find special subStrings in this case with regex?
我有一个字符串,使用正则表达式在字符串末尾提取数字。
字符串:
'0 DB'!$B0
子字符串:
460
我是这样解决的:
String str = "'0 DB'!$B0";
String sStr = str.replaceAll(".*?([0-9]+)$", "");
老问题Link:
现在我有一种不同类型的字符串,我想从中提取某些范围。
字符串:
'0 DB'!$U5:$AH6
在这里,我将提取冒号左侧和冒号右侧的某些区域。
一旦美元符号($)之间的区域,以及它后面的数字。各个区域可以具有不同的长度。第一个美元符号之前的部分可以由字母和数字组成
所以这将是 4 个子字符串。
子字符串:
1: U
2: 305
3: AH
4: 376
我也在考虑用正则表达式解决这个问题。但不幸的是我在这方面的知识有限。
有谁知道如何使用正则表达式解决这个问题?或者还有其他方法吗?
谢谢
对于此要求,您可以简单地使用正则表达式,(?<=\$)\w+
表示 one or more word characters preceded by $
.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "'0 DB'!$U5:$AH6";
Matcher matcher = Pattern.compile("(?<=\$)\w+").matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
输出:
U
305
AH
376
另一种选择是使用特定模式将 4 个部分作为捕获组。
^.*?([A-Z])$(\d+):$([A-Z]+)$(\d+)$
说明
^
字符串开头
.*?
以非贪婪的方式匹配除换行符之外的任何字符 0+ 次
([A-Z])$
捕获组 1 中的字符 A-Z 并匹配 $
(\d+):$
抓取 1+ 组 2 匹配 :$
([A-Z]+)$
在组 1 中捕获 1+ 个字符 A-Z 并匹配 $
(\d+)
匹配组 4 中的 1+ 个数字
$
字符串结束
示例代码
String regex = "^.*?([A-Z])\$(\d+):\$([A-Z]+)\$(\d+)$";
String string = "'0 DB'!$U5:$AH6";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}
要同时匹配两个示例字符串,您可以将第二部分设为可选。
^.*?([A-Z])$(\d+)(?::$([A-Z]+)$(\d+))?$
再看一个regex demo
我有一个字符串,使用正则表达式在字符串末尾提取数字。
字符串:
'0 DB'!$B0
子字符串:
460
我是这样解决的:
String str = "'0 DB'!$B0";
String sStr = str.replaceAll(".*?([0-9]+)$", "");
老问题Link:
现在我有一种不同类型的字符串,我想从中提取某些范围。
字符串:
'0 DB'!$U5:$AH6
在这里,我将提取冒号左侧和冒号右侧的某些区域。
一旦美元符号($)之间的区域,以及它后面的数字。各个区域可以具有不同的长度。第一个美元符号之前的部分可以由字母和数字组成
所以这将是 4 个子字符串。
子字符串:
1: U
2: 305
3: AH
4: 376
我也在考虑用正则表达式解决这个问题。但不幸的是我在这方面的知识有限。
有谁知道如何使用正则表达式解决这个问题?或者还有其他方法吗?
谢谢
对于此要求,您可以简单地使用正则表达式,(?<=\$)\w+
表示 one or more word characters preceded by $
.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "'0 DB'!$U5:$AH6";
Matcher matcher = Pattern.compile("(?<=\$)\w+").matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
输出:
U
305
AH
376
另一种选择是使用特定模式将 4 个部分作为捕获组。
^.*?([A-Z])$(\d+):$([A-Z]+)$(\d+)$
说明
^
字符串开头.*?
以非贪婪的方式匹配除换行符之外的任何字符 0+ 次([A-Z])$
捕获组 1 中的字符 A-Z 并匹配$
(\d+):$
抓取 1+ 组 2 匹配:$
([A-Z]+)$
在组 1 中捕获 1+ 个字符 A-Z 并匹配$
(\d+)
匹配组 4 中的 1+ 个数字$
字符串结束
示例代码
String regex = "^.*?([A-Z])\$(\d+):\$([A-Z]+)\$(\d+)$";
String string = "'0 DB'!$U5:$AH6";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}
要同时匹配两个示例字符串,您可以将第二部分设为可选。
^.*?([A-Z])$(\d+)(?::$([A-Z]+)$(\d+))?$
再看一个regex demo