在Java/Groovy中从字符串中获取前导alpha字符

在Java/Groovy中从字符串中获取前导alpha字符,java,groovy,Java,Groovy,使用Java或Groovy从字符串中获取前导alpha字符的最佳方法是什么?是否有一种使用正则表达式解析的方法,或者我应该使用for..loop构建一个包含前导字母数字的字符串,直到找到一个数字字符?如果是正则表达式,那么正则表达式解析会是什么样子呢 我有这种类型的字符串TT1703408513T,只需要TT的前导字符,但是前导字母字符串的长度可以是1-5个字符,直到找到一个数值为止 更新:这可能不是最有效的方法,但它是有效的 String i= "TT1703408513T"; String

使用Java或Groovy从字符串中获取前导alpha字符的最佳方法是什么?是否有一种使用正则表达式解析的方法,或者我应该使用for..loop构建一个包含前导字母数字的字符串,直到找到一个数字字符?如果是正则表达式,那么正则表达式解析会是什么样子呢

我有这种类型的字符串TT1703408513T,只需要TT的前导字符,但是前导字母字符串的长度可以是1-5个字符,直到找到一个数值为止

更新:这可能不是最有效的方法,但它是有效的

String i= "TT1703408513T";
String o= "";
for( int x; x<i.length(); x++){
   if( i[x].matches("^[a-zA-Z]") )
      o += i[x];
   else
      break;
}
println o;
String i=“TT170340851T”;
字符串o=“”;
对于(intx;xGroovy)
给定

以及所有可接受的字符:

def chars = ['A'..'Z','a'..'z'].flatten()
你可以这么做

def prefix = i.takeWhile { it in chars }
或者,如果您非常想使用正则表达式,可以执行以下操作:

def prefix = i.find(/\p{Alpha}+/)
或者实际上:

a.find(/[A-Za-z]+/)
regexp变体

String i= "TT1703408513T"

def v1 = ( i=~/^[a-zA-Z]+/ ).find{true}
println v1

def v2 = i.replaceAll("^([a-zA-Z]+).*",'$1')
println v2

[Java]

下面给出了一个Java解决方案:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String[] testStrs = { "A1703408513T", "AB1703408513T", "ABC1703408513T", "ABCD1703408513T", "ABCDE1703408513T",
                "ABCDEF1703408513T", "ABCDEFG1703408513T", "1703408513T", "2A703408513T", "@1703408513T",
                "!@£$%1703408513T" };
        String regexToMatch = "[a-zA-Z]{1,5}\\d+.*";
        String regexToExtract = "[a-zA-Z]{1,5}";
        Pattern pattern = Pattern.compile(regexToExtract);
        Matcher matcher;
        for (int i = 0; i < testStrs.length; i++) {
            matcher = pattern.matcher(testStrs[i]);
            if (testStrs[i].matches(regexToMatch) && matcher.find()) {
                System.out.println(matcher.group());
            } else {
                System.out.println(testStrs[i] + " does not meet the criteria");
            }
        }
    }
}
Java解决方案 我面临同样的问题,需要从字符串中提取前导alpha字符。 我用了一个小技巧来达到同样的效果

假设errorCode是我们试图从中获取前导字符的字符串

String errorCode = "AC0000XYZ";
String httpErrorCode =  errorCode.replaceAll("[^A-Za-z]+", " ").split(" ")[0];
//httpErrorCode  would contain "AC" now
它首先用空格替换所有非alpha字符,然后在空格上拆分字符串,然后从中选择第一个元素


唯一不起作用的地方是当字符串中没有前导alpha字符时

“最佳”使用哪种度量?手动循环或使用正则表达式都可以,但为了简单和紧凑起见,我只使用正则表达式。谢谢,如果只提取[a-zA-Z],正则表达式会是什么样子在不知道前导字母字节的实际长度的情况下达到一个数字值?
^[a-zA-Z]+
可以做到这一点,我尝试了这一点,并且
def prefix=i.takeWhile{it in chars}
返回一个空字符串…测试中什么是
i
和什么是
chars
works@ou_ryperd@tim_yates我不想在这里重复这一点。我在这里开始聊天,在那里我可以添加一张图片:
A
AB
ABC
ABCD
ABCDE
ABCDEF1703408513T does not meet the criteria
ABCDEFG1703408513T does not meet the criteria
1703408513T does not meet the criteria
2A703408513T does not meet the criteria
@1703408513T does not meet the criteria
!@£$%1703408513T does not meet the criteria
String errorCode = "AC0000XYZ";
String httpErrorCode =  errorCode.replaceAll("[^A-Za-z]+", " ").split(" ")[0];
//httpErrorCode  would contain "AC" now