Extract json data from given string - java

I am having a string something like this :
a.b.c.d.e =
{"altImages":2,"available":1,"availableColorCount":3};
Now I only need to fetch :
{"altImages":2,"available":1,"availableColorCount":3}
What should be regex expression to extract that part from given string. Please help
My Try :
(?smi)a.b.c.d\\(.*\"e\"=(.*?)\\}\\);.*
But its not helping around.

Try this:
.+\s*=\s*({(?:.+:.+,?)+})(?=;)

You can use something like:
.*?\n(.*);
Here is the version with named groups:
String text = "a.b.c.d.e = \n{\"altImages\":2,\"available\":1,\"availableColorCount\":3};";
Pattern pattern = Pattern.compile(".*?\n(?<JSON>.*);");
Matcher matcher = pattern.matcher(text);
if (matcher.matches()) {
System.out.println(matcher.group("JSON"));
}

Related

Parsing a String using Java regex

I have the below java string in the below format.
externalCustomerID: { \"custToken\": \"xyz\" }
I want to extract xyz value from above string.
can anyone suggest me any regex expression for that in java?
check this one
Pattern pattern = Pattern.compile("(\\w+: \\{ \"\\w+\": \")(\\w+)");
Matcher matcher = pattern.matcher("externalCustomerID: { \"custToken\": \"xyz\" }");
if (matcher.find()) {
System.out.println(matcher.group(2));
}

What is the regex for date type

I have one string I want to find out all date values and relace them with specific string.
My code looks like:
String mydata = "{[... \"date\":\"2016-03-16T12:38:28.390Z\"]},{[ ... \"date\":\"2016-03-16T12:38:28.390Z\" ...]}";
Pattern pattern = Pattern.compile("");
Matcher matcher = pattern.matcher(mydata);
while(matcher.find()){
mydata = mydata.replace(matcher.group(), matcher.group().substring(0, 10));
}
System.out.println(mydata);
What string regex I should pass in Pattern.compile("");?
My output should look like:
{[... "date":"2016-03-16"]},{[ ... "date":"2016-03-16" ...]}
import org.json.*;
JSONObject obj = new JSONObject("{[... "date":"2016-03-16"]},{[ ... "date":"2016-03-16" ...]}");
The rest of the code depends on your json-structure. Have a look at:
How to parse JSON in Java
or
Java Api Link JsonObject
I am not sure about T and Z from input. If they are same always, then below regex will work. If T and Z not constant then change T and Z by [A-Z] in regex.
one more change i did, replaced
mydata = mydata.replace(matcher.group(), matcher.group().substring(0, 10));
by
mydata = matcher.replaceAll("");
It getting required output.
String mydata = "{[\"date\":\"2016-03-16T12:38:28.390Z\"]},{[\"date\":\"2016-03-16T12:38:28.390Z\"]}";
Pattern pattern = Pattern.compile("T\\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{3}Z");
Matcher matcher = pattern.matcher(mydata);
while(matcher.find()){
mydata = matcher.replaceAll("");
}
System.out.println(mydata);
If you want a regex based solution this seems to work for your example
Pattern pattern = Pattern.compile("(\\{\\[.*?\"date\":\"\\d{4}\\-\\d{2}\\-\\d{2}).*?(\"\\]\\})");
Matcher matcher = pattern.matcher(mydata);
while(matcher.find()) {
System.out.println(matcher.group(1) + matcher.group(2));
}
IDEONE DEMO

Regex - find data inside left and right encloses

I have this string:
text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz
I need to extract 123+456+789
What I done so far is:
String s = "text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz";
String ps = "text=(.*)&";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(s);
if (m.find()){
System.out.println(m.group(0));
System.out.println(m.group(1));
}
And I got all text until the last & which is: 123+456+789&xxxxxxxxx&yyyyyyyyyy while the requested output is: 123+456+789
Any suggestions how to fix it (regex is mandatory)?
Use a negated character class:
String ps = "text=([^&]*)";
The value you need will be in Group 1.
The [^&] matches any character but an ampersand.
You almost getting, you need to make your regex lazy (or non greedy) like this:
String ps = "text=(.*?)&";
here ---^
Working demo
Try this regex :
([0-9+]+)
Link : https://regex101.com/r/xU2zF4/1
java code :
String s = "text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz";
String ps = "([0-9+]+)";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(s);
if (m.find()){
System.out.println(m.group(0)); // value of s
System.out.println(m.group(1)); // returns 123+456+789
}

How to delete <script>..</script> by regexp in Java?

Now I have this:
String s = "1<script type='text/javascript'>2</script>3<script type='text/javascript'>3</script>5";
Pattern pattern = Pattern.compile("<script.*</script>");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
s = s.replace(matcher.group(), "");
}
System.out.println(s);
The result is
15
But I need
135
In PHP we have /U modificator, but what should I do in Java? I thought about sth like this, but it is incorrect:
Pattern pattern = Pattern.compile("<script[^(script)]*</script>");
<script([^>]*)?>.*?<\/script>
Try this.You needed a ? for lazy match or shorter match.
See demo.
http://regex101.com/r/kO7lO2/3
replaceAll the below regex by empty string:
<script [^>]*>[^<]*</script>

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 ?
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.
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);.
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/
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").
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
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)"

Categories