Extract Text and Values from String

30 views (last 30 days)
Chad
Chad on 23 Apr 2025 at 0:05
Answered: Walter Roberson on 23 Apr 2025 at 0:34
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]

Accepted Answer

Matt J
Matt J on 23 Apr 2025 at 0:22
Edited: Matt J on 23 Apr 2025 at 0:28
str = 'AB(16.7)CD[20.6]EF[.1] 864.4 Round';
S=strsplit(str,{'(',')','[',']',' '});
Types=S([1,3,5])
Types = 1x3 cell array
{'AB'} {'CD'} {'EF'}
Vals=str2double(S([2,4,6]))
Vals = 1×3
16.7000 20.6000 0.1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

More Answers (2)

Image Analyst
Image Analyst on 23 Apr 2025 at 0:31
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))
val1 = 16.7000
type2 = str(9:10)
type2 = 'CD'
val2 = str2double(str(12:15))
val2 = 20.6000
type3 = str(17:18)
type3 = 'EF'
val3 = str2double(str(20:21))
val3 = 0.1000
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)
numbers = 7x1 cell array
{'16' } {'7' } {'20' } {'6' } {'1' } {'864'} {'4' }
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
val1 = 16.7000
val2 = str2double(numbers{3}) + str2double(numbers{4}) / 10
val2 = 20.6000
val3 = str2double(numbers{5}) / 10
val3 = 0.1000
patLetters = lettersPattern;
letters = extract(str,patLetters)
letters = 4x1 cell array
{'AB' } {'CD' } {'EF' } {'Round'}
type1 = letters{1}
type1 = 'AB'
type2 = letters{2}
type2 = 'CD'
type3 = letters{3}
type3 = 'EF'
Again, perhaps not as compact as regexp but I'd go for understandability, readability, and intuitiveness over compactness every time.

Walter Roberson
Walter Roberson on 23 Apr 2025 at 0:34
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
info = struct with fields:
type1: 'AB' val1: '16.7' type2: 'CD' val2: '20.6' type3: 'EF' val3: '.1'
info = struct with fields:
type1: 'AB' val1: '16' type2: 'CD' val2: '20.6' type3: 'EF' val3: '.1'

Categories

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

Tags

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!