我想删除第一个数字序列结束后的所有字符。删除特定序列后的所有字符
编辑:(对不起,不确定性)
- 字符串可能会或可能不会与字母开头,但如果它开始 字母应予以保留。
第一个数字序列后,应删除所有内容。
例如QWEE1232RTY - > QWE1232
123RTY - > 123
12342 - > 12342
str.replaceAll("[^A-Z][^0-9]+","");
这应该工作,但它给人意想不到的输出,而不是打印重复 “2”。
My ideone code for the attempt and it's respective outputs
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String str = "BCTC27452ASDFCCC";
System.out.println(str.replaceAll("[^A-Z0-9]",""));
// BCTC27452ASDFCCC
System.out.println(str.replaceAll("[^A-Z0-9]$",""));
// BCTC27452ASDFCCC
System.out.println(str.replaceAll("[^A-Z][^0-9]",""));
// BCTC2745SDFCCC
System.out.println(str.replaceAll("[^A-Z][^0-9]+",""));
// Closest Output - "BCTC2745" (why 2nd "2" not printed)
System.out.println(str.replaceAll("[^A-Z][^0-9]*",""));
// BCTC
System.out.println(str.replaceAll("[^A-Z][^0-9+]",""));
// BCTC2745SDFCCC
System.out.println(str.replaceAll("[^A-Z][^0-9*]",""));
// BCTC2745SDFCCC
System.out.println("Expected Output: BTC27452");
}
}
为什么不提取['^ [AZ] {4} [0-9] {5}'](https://regex101.com/r/aE5cN9/2) – Tushar
你的问题是为什么'2'是去掉?你能澄清规则吗? –
@Tushar字母数量不固定。 – sian