I have a very simple regex similar to this:
HOHO.*?_HO_
With this test string...
fiwgu_HOHO_HOHO_HOHOrgh_HOHO_feh_HOHO___HO_fbguyev
- I expect it to match just
_HOHO___HO_
(shortest match, non-greedy) - Instead it matches
_HOHO_HOHO_HOHOrgh_HOHO_feh_HOHO___HO_
(longest match, looks greedy).
Why? How can I make it match the shortest match?
Adding and removing the ?
gives the same result.
Edit - better test string that shows why [^HOHO]
doesn't work: fiwgu_HOHO_HOHO_HOHOrgh_HOHO_feh_HOHO_H_O_H_O_HO_fbguye
All I can think of is that maybe it is matching multiple times - but there's only one match for _HO_
, so I don't understand why it isn't taking the shortest match that ends at the _HO_
, discarding the rest.
I've browsed all the questions I can find with titles like "Non-greedy regex acts greedy", but they all seem to have some other problem.
HOHO
; and it can do this without backtracking, so it won't try to shorten that. – Oestrin