Java 将正则表达式添加到列表以进行排序

Java 将正则表达式添加到列表以进行排序,java,regex,arrays,list,Java,Regex,Arrays,List,我在这里试图做的是对正则表达式的结果进行排序(例如,如果它们是数字)。我不知道该怎么做,有什么想法吗 NodeList abcList = firstElement.getElementsByTagName("target"); Element abcElement =(Element)abcList.item(0); NodeList textAbcList = abcElement.getChildNodes(); String abc = (textAbcList.item(0).getN

我在这里试图做的是对正则表达式的结果进行排序(例如,如果它们是数字)。我不知道该怎么做,有什么想法吗

NodeList abcList = firstElement.getElementsByTagName("target");
Element abcElement =(Element)abcList.item(0);
NodeList textAbcList = abcElement.getChildNodes();
String abc = (textAbcList.item(0).getNodeValue().trim());
Pattern pattern = Pattern.compile("Some Regex");
Matcher matcher = pattern.matcher(abc);
while (matcher.find()){
out.write(" abc: " + matcher.group());
}

查找

要对结果进行排序,您需要首先找到所有结果。如果事先不知道所有结果,可以生成任何部分排序的列表。所以你会有这样的想法:

List<Integer> results = new ArrayList<Integer>();
while (there are more results) { // here you ask the regex if it found some more item
   // add integer to results
   String found = ... // here you grab the string you've just found
   results.add(Integer.parseInt(found)); // convert the string to integer and add to list
}
Collections.sort(results, new Comparator<String>() {
  public int compare(String str1, String str2) {
     // obtain the number you'll use to compare
     int value1 = getImportantNumber(str1);
     int value2 = getImportantNumber(str2);
     // return comparator (remember, the sign of the results says if it's <, =, >)
     return value1 - value2;
  }

  // this method will extract the number, maybe you'll need a regex or substring, dunno
  private int getImportantNumber(String str) {
     // by example
     Matcher m = PATTERN.matcher(str);
     if (!m.find())
        return -1; // or throw an exception, depends on you're requirements
     String numberPart = m.group(...); // the number of the group catching the part you need
     return Integer.parseInt(numberPart);
  }

  private static Pattern PATTERN = Pattern.compile("...."); 
});
此方法将实现mergesort(如果我没有弄错的话),并在每次需要比较两个元素时询问您提供的比较器。这个比较器应该实现接口
comparator
,其中
T
是结果中元素的类型

如果它们是整数,则不需要比较器,因为它已经具有“自然”顺序:

但是,如果您需要一些特殊的排序(如根据其整数表示的值对字符串进行排序),则可以使用您自己的比较器:

Collections.sort(results, new Comparator<String>() {
   public int compare(String a, String b) {
      int valueA = Integer.parseInt(a);
      int valueB = Integer.parseInt(b);
      return valueA - valueB;
   }
});
哪个正则表达式

我应该使用:

(\w+)-(\d+)(-(\d+))*
结果发现:

letters-numbers[-numbers[-numbers...]]
但是如果你不确定第二位的数字,我应该选择:

String[] parts = str.split("-");
for (String part: parts)
   if (this part has only numbers)
      return Integer.parseInt(part);
// if there are no only number parts
throw new RuntimeException("Not valid number part found!");

让我理解一下:你想以某种方式对结果列表进行排序(来自正则表达式搜索)?在这种情况下,有两个截然不同的问题:查找和排序。您需要在列表中首先显示所有结果。然后分类。请澄清您需要什么,然后我们可以回答不同的问题…我正在解析xml和检索数字。我需要对这些数字进行排序。好的。请记住,XML解析器将更好地解析XML。如果您的xml中有一些非xml文本,并且也需要进行解析,则正则表达式就可以了。是的,我现在正在检索非xml文本,我需要对其进行排序。谢谢我的回答。我假设您可以从正则表达式解析器中检索所有结果。对不起,我应该说,我检索的部分是abc-1234-1234,而不仅仅是一个数字,所以我需要按第一组数字排序。在这种情况下,您有兴趣持有
abc-1234-5678
,并希望根据
1234
对整个字符串进行排序。。。如果您确认,我将更改帖子:)
(\w+)-(\d+)(-(\d+))*
letters-numbers[-numbers[-numbers...]]
String[] parts = str.split("-");
for (String part: parts)
   if (this part has only numbers)
      return Integer.parseInt(part);
// if there are no only number parts
throw new RuntimeException("Not valid number part found!");