Prasanna
Prasanna

Reputation: 1677

Extracting a pattern from String

I have a Random string from which i need to match a certain pattern and parse it out.

My String-

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

From the above I want to parse out (using regex) all the values for userId attribute. Can anyone help me out on how to do this ? It is a Random string and not JSON. Can you provide me a regex solution for this ?

Upvotes: 0

Views: 129

Answers (6)

Akram Fares
Akram Fares

Reputation: 1683

It's a JSON format, so you have to use a JSON Parser:

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

EDIT : Regex pattern

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

Upvotes: 0

Christopher Stock
Christopher Stock

Reputation: 1437

Here's the regex-code you're asking for ..

    //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 ) + "]" );
    }

But OF COURSE I´d suggest to solve this using a JSON-Parser like http://json.org/java/

Greetings Christopher

Upvotes: 0

ssssteffff
ssssteffff

Reputation: 974

As other already told you, it looks like a JSON String, but if you really want to parse this string on your own, you could use this piece of code:

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

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

The matcher will match every "userId":"12345" pattern. matcher.group(1) will return every userId, 12345 in this case (matcher.group() without parameter returns the entire group, ie "userId":"12345").

Upvotes: 0

assylias
assylias

Reputation: 328598

To get the user Ids, you can use this pattern:

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

which outputs:

255863
5216907
5216907

If you want the full string "userId":"xxxx", you can use m.group(); instead of m.group(1);.

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68393

Use JSON parser instead of using Regex, your code will be much more readable and maintainable http://json.org/java/

https://code.google.com/p/json-simple/

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272257

Is that a random string ? It looks like JSON to me, and if it is I would recommend a JSON parser in preference to a regexp. The right thing to do when faced with a particular language/grammar is to use the corresponding parser, rather than a (potentially) fragile regexp.

Upvotes: 3

Related Questions