Click here to Skip to main content
15,392,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello sir i have to access all string which is come after "text":

i have to extract- "SAP Remote Basis Support @ $2.29\/hour\/per server. 24x7x365 Support. http:\/\/t.co\/oFuPepEB8c" this statement occur "text" attribute

from

[{"created_at":"Tue Aug 27 21:18:38 +0000 2013","id":372468218289340416,"id_str":"372468218289340416","text":"SAP Remote Basis Support @ $2.29\/hour\/per server. 24x7x365 Support. http:\/\/t.co\/oFuPepEB8c","source":"\u003ca href=\"http:\/\/www.tweetdeck.com\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e"]



please help me with regular expression or other method
Posted
Updated 28-Aug-13 21:26pm
v2
Comments
David Goebet 29-Aug-13 3:27am
   
if its always the same syntax (the same string parts like "SAP" and ,"source"
make a substring from the startpoint("SAP") to the (,"source") Endpoint

1 solution

Try this:
C#
string result = Regex.Match(input, "\"text\":\"(?<txt>[^,\"]+)\"").Groups["txt"].Value;

If you have more than one text variable in your input, try this:
C#
string[] results = Regex.Matches(input, "\"text\":\"(?<txt>[^,\"]+)\"").Cast<Match>().Select(x => x.Groups["txt"].Value).ToArray();

Hope this helps.
   
v4
Comments
anshu77 29-Aug-13 3:53am
   
thank you
there is no error but i can not get correct answer.

actuly i have to get all text data in a file
like-- "text": "hello", "Tue Aug 27 21:18:38 +0000 2013","id":372468218289340416,"id_str":"372468218289340416","text":"SAP Remote Basis Support @ $2.29\/hour\/per server. 24x7x365 Support. http:\/\/t.co\/oFuPepEB8c","source":"\u003ca href=\"http:\/\/www.tweetdeck.com\"

here two time "text" occur so i have 2 access answer-- "hello' and "SAP Remote Basis Support @ $2.29\/hour\/per server. 24x7x365 Support. http:\/\/t.co\/oFuPepEB8c"


thank you in advance
Thomas Daniels 29-Aug-13 4:01am
   
I updated my answer.
anshu77 29-Aug-13 4:01am
   
i have to access only that text which is occour only after "text": attribute which is end by comma(,) my file data is ----

[{"created_at":"Tue Aug 27 21:18:38 +0000 2013","id":372468218289340416,"id_str":"372468218289340416","text":"SAP Remote Basis Support @ $2.29\/hour\/per server. 24x7x365 Support. http:\/\/t.co\/oFuPepEB8c","source":"\u003ca href=\"http:\/\/www.tweetdeck.com\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":58554692,"id_str":"58554692"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[{"url":"http:\/\/t.co\/oFuPepEB8c","expanded_url":"http:\/\/bit.ly\/143rFQs","display_url":"bit.ly\/143rFQs","indices":[68,90]}],"user_mentions":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},{"created_at":"Tue Aug 27 19:21:25 +0000 2013","id":372438721015988224,"id_str":"372438721015988224","text":"#WFTCloud caters to clients across industries and geographies. View the pic attached to know some of our clients. http:\/\/t.co\/W4mdialBc3","source":"\u003ca href=\"http:\/\/www.tweetdeck.com\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":58554692,"id_str":"58554692"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"WFTCloud","indices":[0,9]}],"symbols":[],"urls":[],"user_mentions":[],"media":[{"id":372438721024376832,"id_str":"372438721024376832","indices":[114,136],"media_url":"http:\/\/pbs.twimg.com\/media\/BSsrCyNCQAAMa3f.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/BSsrCyNCQAAMa3f.jpg","url":"http:\/\/t.co\/W4mdialBc3","display_url":"pic.twitter.com\/W4mdialBc3","expanded_url":"http:\/\/twitter.com\/wftus\/status\/372438721015988224\/photo\/1","type":"photo","sizes":{"large":{"w":800,"h":700,"resize":"fit"},"medium":{"w":600,"h":525,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":298,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},{"created_at":"Tue Aug 27 19:18:11 +0000 2013","id":372437908432490496,"id_str":"372437908432490496","text":"SAP on-premise to #Cloud: The journey gets smoother. View pic to know how... http:\/\/t.co\/XpCjgTsMDk","source":"\u003ca href=\"http:\/\/www.tweetdeck.com\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":58554692,"id_str":"58554692"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"Cloud","indices":[18,24]}],"symbols":[],"urls":[],"user_mentions":[],"media":[{"id":372437908440879104,"id_str":"372437908440879104","indices":[77,99],"media_url":"http:\/\/pbs.twimg.com\/media\/BSsqTfGCEAADKlq.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/BSsqTfGCEAADKlq.jpg","url":"http:\/\/t.co\/XpCjgTsMDk","display_url":"pic.twitter.com\/XpCjgTsMDk","expanded_url":"http:\/\/twitter.com\/wftus\/status\/372437908432490496\/photo\/1"}]
Thomas Daniels 29-Aug-13 4:04am
   
And what exact text do you want to get from the file?
anshu77 29-Aug-13 4:15am
   
thank you vary much
last query worked

thank you
Thomas Daniels 29-Aug-13 4:16am
   
You're welcome!
anshu77 29-Aug-13 5:08am
   
sir there is another issue

in above file,which i have sent previous comment i have to access only first text's data which is occur only after "created_at". if two times "text": occurs after "created_at" then i have to get first "text": data, not all text's data .
Thomas Daniels 29-Aug-13 5:15am
   
If you just want to get the first "text" variable, try this:

string result = Regex.Match(input, "\"text\":\"(?<txt>[^,\"]+)\"").Groups["txt"].Value;
anshu77 29-Aug-13 5:16am
   
not only first but every first after "created_at":
anshu77 29-Aug-13 5:18am
   
because many times "created_at" occur and many time "text": occurs in each "created_at" and i have to get first "text": after every "created_at"
Thomas Daniels 29-Aug-13 5:46am
   
Try this:

string[] results = Regex.Matches(input, "\"created_at\":\"[^,\"]+\",\"text\":\"(?<txt>[^,\"]+)\"").Cast<Match>().Select(x => x.Groups["txt"].Value).ToArray();
anshu77 29-Aug-13 7:13am
   
sir, this query does not provide anythings. pls send me correct answer
anshu77 29-Aug-13 8:03am
   
this code is not working . because text is not immediate after created_at some mor attribute b/w "created_at" and "text".
give me some solution for it.
Thomas Daniels 29-Aug-13 8:05am
   
I think this code is what you need:
<small>
string[] results = Regex.Matches(input, "\"created_at\":\"[^,\"]+\",(\"[^,\"]+\":\"[^,\"]+\",)?\"text\":\"(?<txt>[^,\"]+)\"").Cast<match>().Select(x => x.Groups["txt"].Value).ToArray();</match></small>
anshu77 29-Aug-13 6:39am
   
reply sir
Thomas Daniels 29-Aug-13 6:43am
   
I have replied! Try this:
<small>
string[] results = Regex.Matches(input, "\"created_at\":\"[^,\"]+\",\"text\":\"(?<txt>[^,\"]+)\"").Cast<Match>().Select(x => x.Groups["txt"].Value).ToArray();
</small>
anshu77 29-Aug-13 7:00am
   
sie, this is not reply anything
Thomas Daniels 29-Aug-13 7:06am
   
Why isn't that 'reply anything'? I gave you code to get all "text" variables after "created_at" variables!
anshu77 29-Aug-13 7:14am
   
sir,
i apply that code but that is not provide anything
anshu77 29-Aug-13 8:11am
   
i need only first text variable after created_at variable if again come created_at then again need only first text variable data, not second text that come same created_at.
like-
1-first created_at have two text then i wil need only one text which occur first and skip second text.
2-if second created_at have one text then i will need one text which occur.
anshu77 29-Aug-13 7:21am
   
sir, muje created_at k baad jo pehla "text": aata he uska data chahiye uske baat agar ek or text aata he uska data ni chahiye. ager uske baad created_at ata he to hi text ka data chahiye
i mean meri file me created_at kai baar aara he to muje jaha b created_at aaya he uske baad bala pehle text ka data chaiye agr usi "creted_at": me doosri bar "text": aata he to uska data ni chahiye
anshu77 29-Aug-13 7:29am
   
sie give me some solution?
anshu77 29-Aug-13 7:48am
   
sir, between "created_at": and "text": may be one or more than one attributes like-id,source,etc.
Thomas Daniels 29-Aug-13 7:58am
   
OK, I understand. Try this:
<small>
string[] results = Regex.Matches(input, "\"created_at\":\"[^,\"]+\",(\"[^,\"]+\":\"[^,\"]+\",)?\"text\":\"(?<txt>[^,\"]+)\"").Cast<match>().Select(x => x.Groups["txt"].Value).ToArray();</match></small>
anshu77 29-Aug-13 8:15am
   
sir this code give some error--
parsing ""created_at":"[^,"]+",("[^,"]+":"[^,"]+",)?"text":"(?[^,"]+)"" - Unrecognized grouping construct.

i thik
<match> and txt should b come as first 3rd code contain
anshu77 29-Aug-13 8:18am
   
and b/w "created_at" and "text" may be one or more than one attributes like id, source,screen,etc. not fix that only 2 or 3 variable will be
Thomas Daniels 29-Aug-13 8:36am
   
I understand. Try this code:
<small>
string[] results = Regex.Matches(input, "\"created_at\":\"[^,\"]+\",(\"[^,\"]+\":\"[^,\"]+\",)*\"text\":\"(?<txt>[^,\"]+)\"").Cast<Match>().Select(x => x.Groups["txt"].Value).ToArray();
</small>
anshu77 29-Aug-13 9:15am
   
sorry sir,there is no error bt this code also send nothing.work not properly
anshu77 29-Aug-13 9:19am
   
sir please try like every text's data new line and then apply split and then access only first text which occur after each created_at
Thomas Daniels 29-Aug-13 9:47am
   
OK, now I see why it is not working. I'm trying to find a new code.
Thomas Daniels 29-Aug-13 10:00am
   
Try this:
<small>
string[] results = Regex.Matches(input, "\"created_at\":\"[^,\"]+\",(\"[^,\"]+\":(\")?[^,\"]+(\")?,)*?\"text\":\"(?<txt>[^,\"]+)\"").Cast<Match>().Select(x => x.Groups["txt"].Value).ToArray();
</small>
anshu77 29-Aug-13 10:39am
   
thank you very much sir. it is worked correctly. thanks a lot.
Thomas Daniels 29-Aug-13 11:44am
   
You're welcome!
anshu77 30-Aug-13 4:47am
   
hello sir, i have send a file above that is json file . now i have to get same answer using dictionary .
i have a json file and i have to get access same answer from json file using dictionary.

thanks in advance.
Thomas Daniels 30-Aug-13 4:57am
   
If you want to parse a JSON file, then you can also use Json.NET:
http://json.codeplex.com/
anshu77 30-Aug-13 5:52am
   
i have already this pakage
Thomas Daniels 30-Aug-13 6:28am
   
anshu77 2-Sep-13 2:41am
   
thank you sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900