Finding location of an exact match in a string

4 views (last 30 days)
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.

Accepted Answer

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

Sign in to comment.

More Answers (1)

Jan
Jan on 4 Jan 2018
Edited: Jan on 4 Jan 2018
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 Comment
Guillaume
Guillaume on 4 Jan 2018
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.

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!