1

Hello Guys i want to get just two value from a big json array using regular expression. E.g: i have this json array

'{"data":"1233","image":"dsfsdfsdfds"."text":"hello world"."name": "java"}' '{"data":"1233df","image":"dsfsdfsdfsdfds"."text":"hello world"."name": "c#"}' '{"data":"1233sds","image":"dsfsdferesdfds"."text":"hello world"."name": "python"}' '{"data":"1sd233","image":"dsfsdfsdfrdfds"."text":"hello world"."name": "c++"}'

I want to get text and name value from this array using regular expression

1
  • 1
    Don't use regex for this. This is a FAQ. (Structured formats like XML and JSON are like HTML in this context.) Commented Jul 23, 2018 at 7:11

3 Answers 3

4

You should not use regex for parsing JSON. Instead parse JSON through inbuilt functions like parse in case you are using Javascript or different libraries like GSON in case you are using JAVA.

Still, if there is any special requirement to use Regex here, you can use below:

1) For text:

.*?text"\s?:\s?"([\w\s]+)

Output :

Group 1 - 'hello world'

2) For name

.*?name"\s?:\s?"([\w\s]+)

Output:

Group 1 - java

Demo: https://regex101.com/r/74hOO1/1

-- EDIT --

If you want both text and name in one regex, you can use below one, provided text always come before name:

.*?text"\s?:\s?"([\w\s]+).*?name"\s?:\s?"([\w\s]+)

Output:

Group 1.    46-57   `hello world`
Group 2.    68-72   `java`

Demo: https://regex101.com/r/74hOO1/2

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

3 Comments

i want them to be in one regular expresion. I mean that in one regular expression i can get text and name ... like every name and his text
not worked with my json format... can you see this (?:"name":")(.*?)(?:") and modified like what i am looking for ?
@Mohamed For the udpated json, I have updated regex as well :) You can check it at regex101.com/r/74hOO1/3
2
var data = '{"data":"1233","image":"dsfsdfsdfds", "text":"hello world", "name": "java"}';

var parsedData = JSON.parse(data);

var text = parsedData.text;
var name = parsedData.name;

2 Comments

Mate +1 for efforts, but OP has not mentioned which language is being used
That is not solution with regex. @AmanChhabra can you explain how to use your solution in SQL?
0

You can use this regex :

"name"\s?+:\s?+"?([\w+#\s]+)"?

to extract its value here name for example

if you want other key for example text use text instead of name

"text"\s?+:\s?+"?([\w+#\s]+)"?

you'll find the value of the key in the first group See here https://regex101.com/r/1ZSUph/1

1 Comment

i want one regular expression that show every name is the text behind

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.