2012-09-28 47 views
2

我在這裏要做的是對正則表達式進行排序(例如,如果它們是數字)。即時通訊不知道如何去做這個,有什麼想法?將正則表達式添加到列表中排序

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()); 
} 
+0

讓我明白了:你想好歹排序(從正則表達式搜索)發現的一個列表?在這種情況下,存在兩個截然不同的問題:查找和排序。你首先需要列出所有結果。然後分類。請澄清你需要什麼,然後我們可以回答不同的問題... – helios

+0

Im解析XML和回顧數字。我需要對這些數字進行排序。 – user1646537

+0

好的。請記住,XML解析器將更好地解析XML。如果你的XML中有一些非xml文本並且需要解析,那麼RegEx會很好。 – helios

回答

2

尋找

排序,你需要找到他們第一個成果。如果您事先不知道所有結果,則可以生成任何部分排序列表。所以你必須是這樣的:

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 
} 

請注意,我找到的字符串直接轉換成整數,因爲它有更多的含義爲整數。如果由於任何原因你想要一個字符串,好吧,有一個List<String>,不要轉換。

排序

之後,你有一個非排序列表,你需要對它進行排序。有幾種方法和Java實現一個非常簡單的方法。它可以對任何類型進行排序,因爲它不會對兩個項目進行比較。這是定義如何分類的唯一部分。你會做:

Collections.sort(results, comparator); 

這種方法將實現合併排序(如果我沒有記錯),並要求你每次需要比較兩個元素時提供的比較。這個比較器應該實現接口Comparator<T>其中T是在結果元素的類型。

如果它們是整數,你並不需要一個比較,因爲它已經「自然」的順序:

Collections.sort(results); 

但是,如果你按照它的整數值表示想要一些特殊的排序(如訂購串),那麼你可以使用自己的比較:

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; 
    } 
}); 

比較必須返回:

  • negat ive if < b
  • 0 if a == b
  • and positive if a> b。

因爲我們想比較字符串,就好像它們是數字一樣,這就是我所做的:將它們轉換爲數字並比較它們的數值。

排序您strigs:XXX-NNNN-NNNN

在你的情況,你正在收集的字符串與格式(ABC-1234-5678),你需要根據第一個數字對它們進行排序。因此,讓我們假設你已經收集到您的字符串:

List<String> results 

然後,你需要的是,根據一些任意的標準字符串進行排序。像往常一樣,你需要調用Collections.sort提供一個特殊的比較。

比較器將不需要比較整個字符串,而是比較每個字符串的第一個數字。例如:abc-1234-5678def-3456-1988。您必須將12343456進行比較。

然後代碼看起來類似:

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("...."); 
}); 

其中正則表達式

我應該使用:

(\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!"); 
+0

對不起,我應該說,im retreiving的部分是abc-1234-1234不只是一個數字,所以我需要按第一組數字排序 – user1646537

+0

在這種情況下,您有興趣拿着'abc-1234-5678',並想根據'1234'來訂購這個完整的字符串......如果您確認我會更改帖子:) – helios

+0

是的,我可以確認。 – user1646537