As explained in https://mathematica.stackexchange.com/a/72293/45020, using Shortest
in StringCases
does not actually return the shortest matching string. Instead it looks until some part matches (from left to right) and then it looks ahead and stops at the first complete match from that position. Of course this misses the true shortest string whenever this first match is not part of the shortest string. See for example
StringCases["This is not what I'm looking for. This is part of the
shortest match", Shortest["This is" ~~ any__ ~~ "match"]]
where the whole string gets matched while a stronger string match is available.
I need the reverse behavior. Where one searches from right to left (Alternatively I need to find the "true" Shortest substring, that would also work for my application).
I have a StringExpression
that describes some pattern, say "out ["~~x: NumberString~~"]", which at some point is preceded by some strong "some_string" from which I want to extract the string between the end of the regular pattern and the closest preceding instance of "some_string". So for example
reverseShortestCase["some_string other stuff I don't want. some_string, the part I do want, out [3.1]", "some_string", "out ["~~x: NumberString~~"]"]
should return "some_string, the part I do want, out [3.1]"
.
How should I go about doing this? I guess I can try to reverse the string and the patterns in order to search right to left? But this seems complicated if you want to be able to use things like NumberString.
The solutions proposed in the linked question do not seem to offer a solution for this case where there is no fixed begin and end string and instead a StringExpression needs to be used to describe these.
StringCases
call returns two matches to the input, which seems like the correct behavior. $\endgroup$StringCases
returns a list of 2 strings, (and not the whole initial string) $\endgroup$StringCases["This is not what I'm looking for. This is part of the shortest match", Shortest["This is" ~~ any__ ~~ "match"]]
which only has one match, and is not the intended match. $\endgroup$