0

I have a string (for example: "foo12"), and I want to add a delimiting character in between the letters and numbers (e.g. "foo|12"). However, I can't seem to figure out what the appropriate code is for doing this in Java. Should I use a regex + replace or do I need to use a matcher?

3 Answers 3

4

A regex replace would be just fine:

 String result = subject.replaceAll("(?<=\\p{L})(?=\\p{N})", "|");

This looks for a position right after a letter and right before a digit (by using lookaround assertions). If you only want to look for ASCII letters/digits, use

String result = subject.replaceAll("(?i)(?<=[a-z])(?=[0-9])", "|");
0

Split letters and numbers and concatenate with "|". Here is a one-liner:

String x = "foo12";

String result = x.replaceAll("[0-9]", "") + "|" + x.replaceAll("[a-zA-Z]", "");

Printing result will output: foo|12

2
  • 1
    This looks very dodgy. What if there is anything else in the string besides letters and digits? What if there is more than one pair? Commented Sep 25, 2014 at 19:59
  • well yeah that's true. I am assuming that the String is exactly in the form that the OP posted in the question.
    – gkrls
    Commented Sep 25, 2014 at 20:00
0

Why even use regex? This isn't too hard to implement on your own:

public static String addDelimiter(String str, char delimiter) {
    StringBuilder string = new StringBuilder(str);
    boolean isLetter = false;
    boolean isNumber = false;

    for (int index = 0; index < string.length(); index++) {
        isNumber = isNumber(string.charAt(index));
        if (isLetter && isNumber) {
            //the last char was a letter, and now we have a number
            //so here we adjust the stringbuilder
            string.insert(index, delimiter);
            index++; //We just inserted the delimiter, get past the delimiter
        }
        isLetter = isLetter(string.charAt(index));
    }
    return string.toString();
}
public static boolean isLetter(char c) {
    return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z';
}
public static boolean isNumber(char c) {
    return '0' <= c && c <= '9';
}

The advantage of this over regex is that regex can easily be slower. Additionally, it is easy to change the isLetter and isNumber methods to allow for inserting the delimiter in different places.

1
  • I doubt that a regex will be that much slower, if at all (and even if it were, it might not matter anyway). One line of (perfectly readable) code vs. 20 lines of (medium complexity) code counts for something, too. And if you also should need to handle non-ASCII characters, then regexes become even more appealing :) Commented Sep 26, 2014 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.