Page 1 of 1

How extract (changeable variable) word & number using regular expression matlab

Posted: Mon Mar 06, 2017 12:34 pm
by Charles
I have more than 10k text files look similar like this, all of them are similar in format but not in size, sometime is bigger or smaller.

Code: Select all

[{u'language': u'english', u'area': 3825.8953168044045, u'class': u'machine printed', u'utf8_string': u'troia', u'image_id': 428035, u'box': [426.42422762784093, 225.33333055900806, 75.15151515151516, 50.909090909090864], u'legibility': u'legible', u'id': 1056659}, {u'language': u'na', u'area': 24201.285583103767, u'id': 1056660, u'image_id': 428035, u'box': [223.99998520359847, 249.57575480143228, 172.12121212121215, 140.6060606060606], u'legibility': u'illegible', u'class': u'machine printed'}]
I want to extract two changeable variable in every text using regular expression.

The output should be like this

Code: Select all

box  = [223.99998520359847, 249.57575480143228, 172.12121212121215, 140.6060606060606]
box1 = .. sometime there is more than one 
& second output

Code: Select all

word = troia 
word1 =  ... sometime there is more than one word 
My code 1: for the word extraction

Code: Select all

fid = fopen('text1.txt','r');
C = textscan(fid, '%s','Delimiter','');
fclose(fid);

C = C{:};

Lia = ~cellfun(@isempty, strfind(C,'utf8_string'));

output = [C{find(Lia)}];
expression = 'u''utf8_string'': u+'
matchStr = regexp(output, expression,'match');
My code 1 result give me only the

Code: Select all

utf8_string
My code 2: for the box number extraction

Code: Select all

s = sprintf('text_.txt'); 
fid = fopen(s);
tline = fgetl(fid);

C = regexp(tline,'u''box'': +\[([0-9\. ,]+)\]','tokens');
C = cellfun(@(x) x{1},C,'UniformOutput',false)';
M = cell2mat(cellfun(@(x) x', cat(1,C2{:}),'UniformOutput',false));
This code 2 is running but not with every text something i got this error

Code: Select all

Error using cat Dimensions of matrices being concatenated are not consistent