Java通过替换特殊字符来转换一组String

时间:2017-05-11 12:58:45

标签: java arrays string list replace

public static void main(String[] args) {
    String str = "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj";
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(str);
    List<String> strings = new ArrayList<String>();
    List<Integer> nums = new ArrayList<Integer>();
    while (m.find()) {
        nums.add(Integer.parseInt(m.group()));
    }
    p = Pattern.compile("[a-z]+");
    m = p.matcher(str);
    while (m.find()) {
        strings.add(m.group());
    }
    System.out.println(nums);
    System.out.println(strings);
}

输出:

[12, 124, 1234, 123, 1234]
[astv, atthh, ggh, dhr, sfff, dgdfg, mnaoj]

但我希望输出如下:

[12124, 1234123, 1234]
[astv, atthhggh, dhrsfff, dgdfg, mnaoj]

3 个答案:

答案 0 :(得分:2)

您可以使用*拆分,然后您可以使用每个元素,例如:

public static void main(String[] args) {
    String str = "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj";
    String[] spl = str.split("\\*");//[astv, 12atthh124ggh, dhr1234sfff123, dgdfg1234, mnaoj]
    List<String> strings = new ArrayList<>();
    List<Integer> nums = new ArrayList<>();
    for (String s : spl) {

        String tmp = s.replaceAll("\\d+", "");//replace all the digits with empty
        if (!tmp.trim().isEmpty()) {
            strings.add(tmp);
        }

        tmp = s.replaceAll("[a-z]+", "");//replace all the character with empty
        if (!tmp.trim().isEmpty()) {
            nums.add(Integer.parseInt(tmp));
        }
    }

    System.out.println(nums);
    System.out.println(strings);
}

<强>输出

[12124, 1234123, 1234]
[astv, atthhggh, dhrsfff, dgdfg, mnaoj]

Ideone demo

答案 1 :(得分:1)

要获得数字数组,您可以

  • 用空字符串替换所有字母
  • 由星号*
  • 拆分

- 代码

String digitArr[] = str.replaceAll("[A-Za-z]", "").split("\\*");
//output
//12124 1234123 1234 

你可以重复同样的事情来获得字母数组

String stringArr[] = str.replaceAll("[0-9]", "").split("\\*");
//Output
//astv atthhggh dhrsfff dgdfg mnaoj 

DEMO

答案 2 :(得分:1)

要保留原始正则表达式逻辑,您可以执行以下操作:

public static void main(String[] args) {
    String str = "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj";
    List<String> strings = new ArrayList<>();
    List<Integer> nums = new ArrayList<>();
    Pattern digitPattern = Pattern.compile("\\d+");
    Pattern alphaPattern = Pattern.compile("[a-z]+");
    String[] splittedArray = str.split("\\*");
    for (String nextSplittedString : splittedArray) {
        Matcher digitMatcher = digitPattern.matcher(nextSplittedString);
        Matcher alphaMatcher = alphaPattern.matcher(nextSplittedString);
        String nextDigitAsString = "";
        while (digitMatcher.find()) {
            nextDigitAsString += digitMatcher.group();
        }
        if (!nextDigitAsString.isEmpty()) {
            nums.add(Integer.parseInt(nextDigitAsString));
        }

        String nextString = "";
        while (alphaMatcher.find()) {
            nextString += alphaMatcher.group();
        }
        if (!nextString.isEmpty()) {
            strings.add(nextString);
        }
     }
     System.out.println(nums);
     System.out.println(strings);
}

<强>输出

[12124, 1234123, 1234]
[astv, atthhggh, dhrsfff, dgdfg, mnaoj]

Ideone demo