dsaba Posted February 13, 2008 Share Posted February 13, 2008 I'd like to know how to use the all character consuming .*? and be able to limit how much it can consume, specifically with a range. Here's a example I made up: i want to match the literal 18 followed by [any amount of characters that is less than 20 characters and not including the literal '12'] followed by the literal 12 in this haystack: 18 hi hellosaf how sdfare asdyou ddsfoing tasdfoday bla bla bla asdfsdfbla bla12 18boblkjasd12 only '18boblkjasd12' should be the wanted match I tried this: ~18(.*?){0,20}12~ However it matched the unwanted one too, hence my question. Thank you. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 13, 2008 Share Posted February 13, 2008 The problem is, that you're using *, ? and the range {0,20} together. The asterisk matches the preceding element 0 or more times, the question mark 0 or 1 time and the range 0 or 20 times. That's one hell of a conflict going on there If you wanna match any element (excluding new line), use the dot. And then specify the range: ~18.{0,20}12~ Quote Link to comment Share on other sites More sharing options...
dsaba Posted February 13, 2008 Author Share Posted February 13, 2008 i wanted it to be lazy, using .* without the ? does not ensure that the number '12' won't be part of the 20 characters it will consume... There was a reason for my question and example I know of this method: (??!12).)* and it will stop before it reaches '12' but still I'd like to see an example of having it lazy and limiting the number of chars it consumes.. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 13, 2008 Share Posted February 13, 2008 Oh, you know more about it than me.. Can't help I'm afraid. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 14, 2008 Share Posted February 14, 2008 /18.{0,20}?12/ Quote Link to comment Share on other sites More sharing options...
dsaba Posted February 15, 2008 Author Share Posted February 15, 2008 Awesome! thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.