Jump to content

using .*? but limiting amount of characters to match


dsaba

Recommended Posts

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.

Link to comment
Share on other sites

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~

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.