2013-07-11 56 views
0

我有一個隨機字符串,從中我需要匹配某個模式並解析出來。從字符串中提取一個模式

我與字符串

{ 「SID」: 「zw9cmv1pzybexi」, 「的parentId」:空, 「時間」:1373271966311, 「顏色」: 「#e94d57」 「用戶id」:「255863 「,」st「:」comment「,」type「:」section「,」cType「:」parent「},{},null,null,null,{」sid「:」zwldv1lx4f7ovx「,」parentId 「:」zw9cmv1pzybexi「,」時間「:1373347545798,」顏色「:」#774697「,」userId「:」5216907「,」st「:」comment「,」type「:」section「 : 「子」},{},NULL,NULL,NULL,NULL,NULL,{ 「SID」: 「zw76w68c91mhbs」, 「parentId的」: 「zw9cmv1pzybexi」, 「時間」:1373356224065, 「顏色」: 「#774697」 ,「userId」:「5216907」,「st」:「comment」,「type」:「section」,「cT ype「:」child「},

從上面我想解析出(使用正則表達式)userId屬性的所有值。任何人都可以幫我解決如何做到這一點?它是一個隨機字符串,而不是JSON。你能爲我提供一個正則表達式解決方案嗎?

+0

,你能告訴我們你已經試過什麼這個解決呢? – DDK

+0

請通過發佈您嘗試過的代碼來幫助自己 –

+0

沒有必要在標題中添加主要標籤。 –

回答

3

這是一個隨機字符串嗎?它看起來像JSON,如果是的話,我會推薦JSON parser優先於正則表達式。面對特定語言/語法時要做的正確事情是使用相應的解析器,而不是(可能)脆弱的正則表達式。

+0

這是一個隨機字符串,而不是JSON。你能爲我提供一個正則表達式解決方案嗎? – Prasanna

0

這是一個JSON格式,所以你必須使用一個JSON解析器:

JSONArray array = new JSONArray(yourString); 
for (int i=0;i<array.length();i++){ 
    JSONObject jo = inputArray.getJSONObject(i); 
    userId = jo.getString("userId"); 
} 

編輯:正則表達式

"userId"[ :]+((?=\[)\[[^]]*\]|(?=\{)\{[^\}]*\}|\"[^"]*\") 
Result : 
"userId" : "Some user ID (numeric or letters)" 
+0

它是一個隨機字符串,而不是JSON。你能爲我提供一個正則表達式解決方案嗎? – Prasanna

+0

看到我編輯的代碼 –

1

要獲得用戶ID,您可以使用此模式:

String input = "{\"sid\":\"zw9cmv1pzybexi\",\"parentId\":null,\"time\":1373271966311,\"color\":\"#e94d57\",\"userId\":\"255863\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"parent\"},{},null,null,null,null,{\"sid\":\"zwldv1lx4f7ovx\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373347545798,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},{},null,null,null,null,null,{\"sid\":\"zw76w68c91mhbs\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373356224065,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},"; 

Pattern p = Pattern.compile("\"userId\":\"(.*?)\""); 
Matcher m = p.matcher(input); 
while (m.find()) { 
    System.out.println(m.group(1)); 
} 

,輸出:如果你想在滿弦"userId":"xxxx"

255863 
5216907 
5216907 

,您可以使用m.group();而不是m.group(1);

+0

謝謝!這符合我的要求。 – Prasanna

0

至於其他已經告訴你了,它看起來像一個JSON字符串,但如果你真的要分析你自己的這個字符串,你可以使用這段代碼:

final Pattern pattern = Pattern.compile("\"userId\":\"(\\d+)\""); 
final Matcher matcher = pattern.matcher(line); 

while (matcher.find()) { 
    System.out.println(matcher.group(1)); 
} 

的匹配將匹配每"userId":"12345"模式。 matcher.group(1)將在本例中返回每個userId,12345matcher.group()無參數返回整個組,即"userId":"12345")。

0

這是您要求的正則表達式代碼。

//assign subject 
    String subject = "{\"sid\":\"zw9cmv1pzybexi\",\"parentId\":null,\"time\":1373271966311,\"color\":\"#e94d57\",\"userId\":\"255863\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"parent\"},{},null,null,null,null,{\"sid\":\"zwldv1lx4f7ovx\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373347545798,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},{},null,null,null,null,null,{\"sid\":\"zw76w68c91mhbs\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373356224065,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},"; 

    //specify pattern and matcher 
    Pattern pat = Pattern.compile("userId\":\"(\\d+)", Pattern.CASE_INSENSITIVE|Pattern.DOTALL); 
    Matcher mat = pat.matcher(subject); 

    //browse all 
    while (mat.find()) 
    { 
     System.out.println("result [" + mat.group(1) + "]"); 
    } 

但當然我倒是建議的使用JSON解析器像 http://json.org/java/

問候 克里斯托弗