Finding location of an exact match in a string
4 次查看(过去 30 天)
显示 更早的评论
I have imported a PDF file as string. I need to match '1.1.2' in the string to find its location. However '1.1.1.2' is also in it.
strfind(str,'1.1.2') returns ans = 71 134. It should only return 134.
How can I exactly match '1.1.2' without changing the PDF file string? Thank you!
----- edit:
I need to find the number using: number = [num2str(x),'.',num2str(y),'.',num2str(z)] since I need to trace multiple numbers in a loop.
0 个评论
采纳的回答
Guillaume
2018-1-4
编辑:Guillaume
2018-1-4
Assuming that the '1.1.2' must be at the beginning of a line in a multiline string, this simple regular expression would work:
loc = regexp(yourstring, '^1\.1\.2[^.0-9]', 'lineanchors')
If the criteria is that '1.1.2' must not be preceded by a 'number dot' nor followed by a 'dot number' then:
loc = regexp(yourstring, '(?<!\d\.?)1\.1\.2[^.0-9]')
2 个评论
更多回答(1 个)
Jan
2018-1-4
编辑:Jan
2018-1-4
List = strfind(str, '1.1.2');
Bad = strfind(str, '.1.1.2');
Match = setdiff(List, Bad + 1);
But this fails for "11.1.2". Which character occurs before the searched string?
List = strfind(str, '1.1.2');
Front = str(List - 1);
Valid = ~ismember(Front, '.1234567890');
Match = List(Valid)
This might work, but smart programmers would use a more powerful (and slower) regexp.
1 个评论
Guillaume
2018-1-4
You state that you have a single string. Therefore, unless '1.1.2' is right at the beginning it is going to be preceded by some characters. You have told use that '1.' before it is not allowed but there may be other patterns that are not allowed. It is possible that the preceding character must always be a newline.
Knowing the exact details would help us.
另请参阅
类别
在 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!