0

I have a String like

[{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldPrice":"0","products":["609","613","617","621","624","628","632"]},{"id":"36","label":"GG","price":"0","oldPrice":"0","products":["608","612","616","620","623","627","631"]},{"id":"152","label":"XGG","price":"0","oldPrice":"0","products":["3713","6577","6578","6579","6580","6581","6582"]}]

What i need is the label values: P, M, G, GG, XGG. i tried to get everything between label and a comma, but its not working

"(label)(.*)(,)"

0

4 Answers 4

4
(?<=label\"\:)\"\w+\"

this will do it

or if you really want to parse it as json

do this

var arr = [];
var jso = [{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldPrice":"0","products":["609","613","617","621","624","628","632"]},{"id":"36","label":"GG","price":"0","oldPrice":"0","products":["608","612","616","620","623","627","631"]},{"id":"152","label":"XGG","price":"0","oldPrice":"0","products":["3713","6577","6578","6579","6580","6581","6582"]}];
console.log(jso.length);
for(var i=0;i<jso.length;i++){
    arr.push(jso[i].label);
}
console.log(arr);

regex demo : http://regex101.com/r/kT9kE6

Sign up to request clarification or add additional context in comments.

Comments

4

This string is in the JSON format and hence it is recommended to use a JSON parser rather than using regex

Read about JSON format in http://www.json.org/.

This link provides explains about JSON in java http://www.json.org/java/

2 Comments

Thank you, i know its in JSON but i cant use a parser, that comes as a String object from a web crawler that i cannot change.
You can parse the JSON content present in a string
0

Your regular expression is greedy.

Change it to lazy

label.*?,

You can also use conditional regex to fetch only the label values

Regex using conditional expression

(?<="label":")\w+

Regex Demo

1 Comment

Krishna M the demo in your link shows only the first occurrence of label is selected
0

You should probably use a JSON parser, however...

This regex will capture all the labels in a group names "labels"

.*?(?:label)":"(?<labels>[^"]+)+.*?

Or depending how you are executing this, just search for this match

(?:label)":"(?<labels>[^"]+)

You can see it working here: http://regex101.com/r/fS8jA8

And the resulting JAVA (generated by regex101):

String re = "(?:label)\\":\\"(?<labels>[^\\"]+)";
String str = "[{\"id\":\"9\",\"label\":\"P\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"606\",\"610\",\"614\",\"618\",\"622\",\"625\",\"629\"]},{\"id\":\"8\",\"label\":\"M\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"607\",\"611\",\"615\",\"619\",\"626\",\"630\"]},{\"id\":\"7\",\"label\":\"G\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"609\",\"613\",\"617\",\"621\",\"624\",\"628\",\"632\"]},{\"id\":\"36\",\"label\":\"GG\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"608\",\"612\",\"616\",\"620\",\"623\",\"627\",\"631\"]},{\"id\":\"152\",\"label\":\"XGG\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"3713\",\"6577\",\"6578\",\"6579\",\"6580\",\"6581\",\"6582\"]}]
";

Pattern p = Pattern.compile(re, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.