使用正则表达式从字符串中拆分数字

时间:2015-01-01 16:34:12

标签: java regex string arraylist

我想从masterArray中的字母中拆分数字,并将它们存储在单独的ArrayList s(splitIntegersplitString)中。目前,我的号码位于splitInteger ArrayList,但splitString ArrayList中的字母未正确分隔。

splitString ArrayList有额外的空格,逗号,ArrayList中元素的顺序不正确。

输出应如下:

[55CC, 1C, 255D, 0F]
[55, 1, 255, 0]
[CC, C, D, F]

目前,输出如下:

[55CC, 1C, 255D, 0F]
[55, 1, 255, 0]
[, CC, , C, , D, , F]

我尝试使用我的removeNumbers变量的正则表达式,但对常规表达式并不了解。

import java.util.ArrayList;

public class SplitArrayExample {

public static void main(String[] args) 
{
    int convertValue = 0;

    // Using Regular Expressions to remove Characters 
    String removeLetters = "([A-z]+)";
    String removeNumbers = "([0-9]+)";

    // This is the master list.
    ArrayList<String> masterArray = new ArrayList<String>();
        masterArray.add("55CC");
        masterArray.add("1C");
        masterArray.add("255D");
        masterArray.add("0F");

    // These are used to split the integer and string characters
    ArrayList<Integer> splitInteger = new ArrayList<Integer>();
    ArrayList<String> splitString = new ArrayList<String>();

    for (String element : masterArray)
    {
        // Used to split the strings
        String[] removingLetterCharacters = element.split(removeLetters);
        String[] removingNumberCharacters = element.split(removeNumbers);

        // This for-each loop removes letter values and converts numbers to integer values
        for(String addObject : removingLetterCharacters)
        {
            convertValue = Integer.parseInt(addObject);
            splitInteger.add(convertValue); 
        }

        // This for-each loop removes the integer values
        for(String addObject : removingNumberCharacters)
        {
            splitString.add(addObject);
        }
    }

    System.out.println(masterArray);
    System.out.println(splitInteger);


    System.out.println(splitString);
    }
}

2 个答案:

答案 0 :(得分:2)

尝试使用String的replaceAll方法,以便从String中删除所有字符或数字,如:

String removeLetters = "([A-Z]+)";//you have typo here note capital Z
for (String element : masterArray) {
    String removingLetterCharacters = element.replaceAll(removeLetters, "");
    String removingNumberCharacters = element.replaceAll(removeNumbers, "");
    convertValue = Integer.parseInt(removingLetterCharacters);
    splitInteger.add(convertValue);
    splitString.add(removingNumberCharacters);
}

Output:
[55CC, 1C, 255D, 0F]
[55, 1, 255, 0]
[CC, C, D, F]

答案 1 :(得分:1)

split用于获取字母列表时,列表将包含原始列表中每个元素的两个元素:一个空字符串,以及实际字符串。

例如,对于元素55CC,字母列表将包含[, CC]

要解决这个问题,一种方法是在将字符串添加到输出列表之前检查字符串是否为空:

for (String addObject : removingLetterCharacters) {
    if(!addObject.isEmpty()) {
        convertValue = Integer.parseInt(addObject);
        splitInteger.add(convertValue);
    }
}

for (String addObject : removingNumberCharacters) {
    if(!addObject.isEmpty()) {
        splitString.add(addObject);
    }
}

但是,如果原始列表具有更复杂的字母和数字组合,则使用split将不起作用。例如,如果列表为[55CC7, 1C, 255D, 0F],则数字列表将包含 5 数字而不是4,因为split方法会将元素55CC7拆分为两个数字557。更好的选择是使用String#replaceAll用空字符串替换模式。在这种情况下,正则表达式可能更简单:

// Using Regular Expressions to remove Characters
String removeLetters = "[A-z]";
String removeNumbers = "\\d";

// This is the master list.
ArrayList<String> masterArray = new ArrayList<String>();
masterArray.add("7CC55");
masterArray.add("1C");
masterArray.add("255D");
masterArray.add("0F");


// These are used to split the integer and string characters
ArrayList<Integer> splitInteger = new ArrayList<Integer>();
ArrayList<String> splitString = new ArrayList<String>();

for (String element : masterArray) {
   String removingLetterCharacters = element.replaceAll(removeLetters, "");
   String removingNumberCharacters = element.replaceAll(removeNumbers, "");
   splitInteger.add(Integer.parseInt(removingLetterCharacters));
   splitString.add(removingNumberCharacters);
}