Extract Text and Values from String
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chad
am 23 Apr. 2025
Beantwortet: Walter Roberson
am 23 Apr. 2025
Dear Forum,
I am trying to extract the following information from this string.
str = 'AB(16.7)CD[20.6]EF[.1] 864.4 Round'
The first thing I did is the following.
vals1 = strsplit(str)
I get as expected.
{'AB(16.7)CD[20.6]EF[.1]'} {'864.4'} {'Roundl'}
What I am having trouble is trying to get the next part.
type1 = AB
val1 = 16.7
type2 = CD
val2 = 20.6
type3 = EF
val3 = .1
If I set
str1 = 'AB(16.7)CD[20.6]EF[.1]'
I can only get this to work
val1 = regexp(str1, '(?<=[)\d+\.\d(?=)','match','once')
val1 = '20.6'
What I don't understand is that is if str1 = 'AB(16)CD[20.6]EF[.1]'
Where there is no decimal I get an error.
I am unsure how to achieve the the text [type1,type2,type3] and three values [val1,val2,val3]
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (2)
Image Analyst
am 23 Apr. 2025
How much does the string vary? Are all the things at fixed, specific locations? If so just use indexing
str = 'AB(16.7)CD[20.6]EF[.1] 864.4 Round';
type1 = str(1:2);
val1 = str2double(str(4:7))
type2 = str(9:10)
val2 = str2double(str(12:15))
type3 = str(17:18)
val3 = str2double(str(20:21))
Sure, it's not as compact as some regexp but way less cryptic and far easier to understand.
If the values and types are not in fixed index locations, you should look at using the more modern patterns instead of the older (and more cryptic/harder to use) regexp function. Also see extract as shown in the examples for digitsPattern and lettersPattern
patNum = digitsPattern;
numbers = extract(str,patNum)
You would then have to use str2double and combine cells that you know belong as parts of the same number, like
val1 = str2double(numbers{1}) + str2double(numbers{2}) / 10
val2 = str2double(numbers{3}) + str2double(numbers{4}) / 10
val3 = str2double(numbers{5}) / 10
patLetters = lettersPattern;
letters = extract(str,patLetters)
type1 = letters{1}
type2 = letters{2}
type3 = letters{3}
Again, perhaps not as compact as regexp but I'd go for understandability, readability, and intuitiveness over compactness every time.
0 Kommentare
Walter Roberson
am 23 Apr. 2025
strs = {'AB(16.7)CD[20.6]EF[.1]', 'AB(16)CD[20.6]EF[.1]'};
for idx = 1 : length(strs)
str1 = strs{idx};
info = regexp(str1, '^(?<type1>\w+)\((?<val1>(\d+(\.\d*)?|\.\d+))\)(?<type2>\w+)\[(?<val2>(\d+(\.\d*)?|\.\d+))\](?<type3>\w+)\[(?<val3>(\d+(\.\d*)?|\.\d+))\]', 'names', 'once')
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!