0

特定のパターンに一致させて解析する必要があるランダム文字列があります。

私のひも~

{"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"},

上記から、(正規表現を使用して) userId 属性のすべての値を解析したいと思います。これを行う方法について誰かが私を助けることができますか? JSON ではなく、ランダムな文字列です。このための正規表現ソリューションを提供してもらえますか?

4

6 に答える 6

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

出力:

255863
5216907
5216907

完全な文字列"userId":"xxxx"が必要な場合は、m.group();代わりにm.group(1);.

于 2013-07-11T09:15:26.660 に答える
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 ) + "]" );
    }

もちろん、 http: //json.org/java/のようなJSON-Parserを使用してこれを解決することをお勧めします

こんにちはクリストファー

于 2013-07-11T09:19:22.877 に答える
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"

于 2013-07-11T09:16:02.327 に答える