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.

采纳的回答

Guillaume
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 个评论
Thom Trentelman
Thom Trentelman 2018-1-4
编辑:Thom Trentelman 2018-1-4
Got it!! Thank you!!
regexp(str,['^',num2str(x),'\.',num2str(y),'\.',num2str(z),'[^.]'],'lineanchors')
x = 1, y = 1, z = 2 ... :)))
Jan
Jan 2018-1-4
编辑:Jan 2018-1-4
+1. "1.1.20" was not caught by the simple strfind methods.

请先登录,再进行评论。

更多回答(1 个)

Jan
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
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by