Extract Text and Values from String
14 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(2 个)
Image Analyst
2025-4-23
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 个评论
Walter Roberson
2025-4-23
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!