2013-02-22 63 views
1

我試圖動態分割字符串,長度由較低的羅馬,我的字符串如下:分割冒號後追加到HTML

I need to call 2 numbers: (i) 1234 (ii) 4567 

有時候,我的數據可能包含這樣也:

1st type: 

I need to call 2 numbers: numbers are (i) 1234 (ii) 4567 

2nd type: 

I need to call my friends: numbers are as follows(only close friends) 

我需要dispaly這樣的數據:

I need to call 2 numbers: 
(i) 1234 
(ii) 4567 

冒號之後,這意味着得到的下羅馬(i)和DIS在下一行播放,然後獲取(ii)較低的羅馬,並將其顯示在下一行,等等。

現在我正在使用regx來檢查我的字符串是否包含冒號(:),如果其中存在匹配regx和display的模式。

它適用於我的初始情況,但對於我提到的兩種類型不起作用。

這是我的代碼:

if(mString.contains(":")){ 
        String [] parts = mString.split (":"); 

        html.append("<p>"+parts [0]+"</p>"); 
        Matcher m = Pattern.compile ("\\([^)]+\\)[^(]*").matcher (parts [1]); 
        while (m.find()) { 

         html.append("<p>"+m.group()+"<br>"); 
        } 
        html.append("</br></p>"); 

編輯:

I need to call 2 numbers: 
    (i) 1234 // it can be alphabets also like abc 
    (ii) 4567 // it can be alphabets also abc 

並且還下部羅馬人可以是N等(ⅰ),(ⅱ),(ⅲ)等

+0

他們總是4位數字,並且他們是唯一可以出現在字符串中的4位數字? – Simon 2013-02-22 21:15:54

+0

它不是關於數字有時也可以是字符串也 – Goofy 2013-02-22 21:16:34

+0

請看到我的編輯 – Goofy 2013-02-22 21:28:20

回答

3

也許這個代碼可以幫到你:

Pattern p = Pattern.compile("^.*:.*([0-9]{4}).*([0-9]{4}).*"); 
Matcher m = p.matcher("I need to call 2 numbers: (i) 1234 (ii) 4567"); 
if (m.find()) { 
    System.out.println(m.group(1)); 
    System.out.println(m.group(2)); 
} 

無論如何,你總是可以使用一個子字符串和indexOf,

+0

它的字符串這樣(i)abc(ii)ccc(iii)gggg我的意思是它也可以是字符串或數字,如何去做呢? – Goofy 2013-02-22 21:17:12

+0

現在只有2行,如果它有3或n個更低的數字?請幫助我 – Goofy 2013-02-22 21:19:21

+0

你需要發佈你要獲得的輸入類型以便進行正則表達式 – PbxMan 2013-02-22 21:19:44