0

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".

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. Thanks for any help.

Y.Liu
  • 54
  • 1
  • 7
  • Please add sample data and expected result – Jens Aug 22 '17 at 11:47
  • Are you just asking how to apply split to a string? If so stringVar.split(regex). – Alex Collins Aug 22 '17 at 11:49
  • What regex should be passed to the split method? I tried the regex shown above, but failed – Y.Liu Aug 22 '17 at 11:54
  • Possible duplicate of [How to split a string between letters and digits (or between digits and letters)?](https://stackoverflow.com/questions/8270784/how-to-split-a-string-between-letters-and-digits-or-between-digits-and-letters) – Aman Aug 22 '17 at 11:55
  • Why downvote? They are not the same. I already looked that question, where all the numbers and letters should be split away. I also tried to learn from that case but failed. – Y.Liu Aug 22 '17 at 12:01
  • 1
    Do you know what [`[A-z]` matches](https://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret/29771926#29771926)? Look what [your regex can match](https://regex101.com/r/thI3Om/1). – Wiktor Stribiżew Aug 22 '17 at 12:05
  • Do you want it to be any combination of numbers and letters after the first set of numbers? For example 123a1a1a1? Or 123a898bcs? – Alex Collins Aug 22 '17 at 12:07
  • It is a typo here, should be A-Z, thanks. – Y.Liu Aug 22 '17 at 12:09

3 Answers3

2

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

you can add it to a list/array using these values

marvel308
  • 10,288
  • 1
  • 21
  • 32
1

You can use a pretty simple pattern : "^(\\d+)(\\w+)" which capture digits as start, and then when letters appear it take word-char

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
azro
  • 53,056
  • 7
  • 34
  • 70
0

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

KDJ
  • 1