我有一个字符串:String test = "A1=CA2=BOA2=RA4=OA11=O";
现在我想要输出:A1,A2,A2,A4,A11
如果我的字符串如下:
String test = "[HEADER_1] [DATA] \n A10=T,(\"Re-N RO-25 M-N P-N (B)\"),1.0:0.8,R=25.0 F-7.829215,-4.032765 A20=B,R2,M=XY,R=29.999999999999996 F564.997550,669.454680";
public static void main(String[] args) {
String test = "A1=CA2=BOA2=RA4=O";
String data[] = test.split("[A-a]\\d{1,100}=");
for (String str : data) {
System.out.println("Split data:"+str);
}
}
那是什么样的正则表达式?
答案 0 :(得分:2)
我不会使用拆分。您想要的部件的正则表达式为/a\d+/i
。如果您使用此解决方案和Create array of regex matches中的解决方案来收集列表中的所有匹配项,则可以轻松创建所需的输出。
在Java-Parlour中转换正则表达式:
public static void main(String[] args) {
String test = "A1=CA2=BOA2=RA4=OA11=O";
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("(a\\d+)", Pattern.CASE_INSENSITIVE).matcher(test);
while (m.find()) {
allMatches.add(m.group());
}
}
allMatches现在包含A1 A2 A2 A4 A11
答案 1 :(得分:0)
您可能正在寻找的正则表达式是[A-a][0-9]{1,2}
。
所以在Java中,
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String test = "A1=CA2=BOA2=RA4=OA11=O";
Matcher m = Pattern.compile("[A-a][0-9]{1,2}").matcher(test);
while (m.find()) {
System.out.print(m.group(0)+",");
}
答案 2 :(得分:0)
String test = "A1=CA2=BOA2=RA4=OA11=O";
String[] parts = test.split("=");
String[] output = new String[parts.length];
for (int i=0; i < parts.length; ++i) {
output[i] = parts[i].replaceAll("^.*(A\\d*)$", "$1");
System.out.println(parts[i] + " becomes " + output[i]);
}
<强>输出:强>
A1 becomes A1
CA2 becomes A2
BOA2 becomes A2
RA4 becomes A4
OA11 becomes A11
O becomes O