How to find the following pattern in my Char variable in the workspace?

1 view (last 30 days)
Dear All,
I am reading a webpage through data = webread(url); which giving me a Char variable in the workspace called data.
I want ot get all "Strings" inside that char variable that start and end with "SomeText I dont Care":{"raw": SomeSTuff I dont Care }
E.g.: "costOfRevenue":{"raw":4751000000,"fmt":"4,75B","longFmt":"4.751.000.000"},"totalOtherIncomeExpenseNet":{"raw":-213000000,"fmt":"-213M","longFmt":"-213.000.000"}, and so on.............
How can i do this in Matlab?
As you can see it is a webpage so it containes other stuff that i dont care about
Best Regards
  7 Comments
Bob Thompson
Bob Thompson on 2 Mar 2021
Have you tried using regexp? I would imagine that would allow you to parse out exactly what you want from the website. Something like the following:
raw = regexp(Data,'("costOfRevenue":{"raw".*})','tokens');
I don't expect that to work perfectly the first attempt, I haven't tested it, but it should get you going somewhere.
Obay
Obay on 4 Mar 2021
Yes i have been trying with the regexp function but still not sure how to use it and that is why i am asking here.
Using your suggestion don not answer my question exactly as my question was:
i don't want to give any text value at the start which means in need something like:
raw = regexp(Data,'("*":{"raw".*})','tokens');% so the Text at the start is not impotant as i need all the values of those texts
% this is not giving me the data i need as also it is not stoping at the expected point in need it to stop at } or ,

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 4 Mar 2021
Edited: Stephen23 on 4 Mar 2021
Assuming no nested curly braces:
str = '"costOfRevenue":{"raw":4751000000,"fmt":"4,75B","longFmt":"4.751.000.000"},"totalOtherIncomeExpenseNet":{"raw":-213000000,"fmt":"-213M","longFmt":"-213.000.000"}, and so on.............';
rgx = '"\w+":\{"raw":[^}]+\}';
out = regexp(str,rgx,'match');
out{:}
ans = '"costOfRevenue":{"raw":4751000000,"fmt":"4,75B","longFmt":"4.751.000.000"}'
ans = '"totalOtherIncomeExpenseNet":{"raw":-213000000,"fmt":"-213M","longFmt":"-213.000.000"}'
  1 Comment
Obay
Obay on 11 Mar 2021
Dear Stephan,
Thanks alot that is what i was looking for :)
sorry that i don't really get thos reguler expristion stuff but it is just what i was looking for :)

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!