Find location of exact string
4 次查看(过去 30 天)
显示 更早的评论
Using this line fo code to find "Sine Delta 2" in my xml file which also contains "Cosine Delta 2" and the code returns locaion for both instead of just for "Sine".
How do i return location of the exact string?
row_idx_SineDelta2 = find(~cellfun('isempty',strfind(data,'sine delta 2')))
采纳的回答
Jorg Woehl
2021-3-11
编辑:Jorg Woehl
2021-3-11
regexp(data, '\<sine delta 2')
The \< indicates that the search string must occur at the beginning of a new word - see MATLAB regexp for the almost limitless possibilities for pattern matching.
0 个评论
更多回答(1 个)
Jorg Woehl
2021-3-11
编辑:Jorg Woehl
2021-3-11
Starting with R2020b, you can use pattern with strfind, which allows you to only find matches if they are preceded by a nonletter character (i.e. if the search string starts a new word):
pat = letterBoundary + 'sine delta 2';
strfind(data, pat)
Note that this is case-sensitive; in your code you are searching for 'sine delta 2', but you mention 'Sine Delta 2' in your introduction...
2 个评论
另请参阅
类别
在 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!