简体   繁体   English

如何在Java中使用正则表达式拆分包含数字和字母的字符串

[英]How to use regex to split a string containing numbers and letters in java

My task is splitting a string, which starts with numbers and contains numbers and letters, into two sub-strings.The first one consists of all numbers before the first letter. 我的任务是将以数字开头,包含数字和字母的字符串分成两个子字符串。第一个子字符串包括第一个字母之前的所有数字。 The second one is the remained part, and shouldn't be split even if it contains numbers. 第二个是剩余部分,即使包含数字也不应该拆分。

For example, a string "123abc34de" should be split as: "123" and "abc34de". 例如,字符串“ 123abc34de”应拆分为:“ 123”和“ abc34de”。

I know how to write a regular expression for such a string, and it might look like this: 我知道如何为这样的字符串编写正则表达式,它可能看起来像这样:

[0-9]{1,}[a-zA-Z]{1,}[a-zA-Z0-9]{0,}

I have tried multiple times but still don't know how to apply regex in String.split() method, and it seems very few online materials about this. 我已经尝试了很多次,但仍然不知道如何在String.split()方法中应用正则表达式,关于此的在线材料似乎很少。 Thanks for any help. 谢谢你的帮助。

you can do it in this way 你可以这样

final String regex = "([0-9]{1,})([a-zA-Z]{1,}[a-zA-Z0-9]{0,})";
final String string = "123ahaha1234";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

matcher.group(1) contains the first part and matcher.group(2) contains the second matcher.group(1)包含第一部分,matcher.group(2)包含第二部分

you can add it to a list/array using these values 您可以使用这些值将其添加到列表/数组

You can use a pretty simple pattern : "^(\\\\d+)(\\\\w+)" which capture digits as start, and then when letters appear it take word-char 您可以使用一个非常简单的模式: "^(\\\\d+)(\\\\w+)" ,将数字作为开始,然后在出现字母时将其取为字符

String string = "123abc34de";
Matcher matcher = Pattern.compile("^(\\d+)(\\w+)").matcher(string);
String firstpart = "";
String secondPart = "";

if (matcher.find()) {
    firstpart = matcher.group(1);
    secondPart = matcher.group(2);
}
System.out.println(firstpart + " - " + secondPart); // 123 - abc34de

This is not the correct way but u will get the result 这不是正确的方法,但您会得到结果

public static void main(String[] args) {
    String example = "1234abc123";
    int index = 0;
    String[] arr = new String[example.length()];
    for (int i = 0; i < example.length(); i++) {
        arr = example.split("");
        try{
        if(Integer.parseInt(arr[i]) >= 0 & Integer.parseInt(arr[i]) <= 9){
            index = i;
        }
        else
            break;
        }catch (NumberFormatException e) {
            index = index;
        }
    }
    String firstHalf = example.substring(0,Integer.parseInt(arr[index])+1);
    String secondHalf = example.substring(Integer.parseInt(arr[index])+1,example.length());
    System.out.println(firstHalf);
    System.out.println(secondHalf);
}

Output will be: 1234 and in next line abc123 输出将是:1234和下一行abc123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM