I often find myself hoping to check a regex string for something else BEFORE something occurs. For example: - I want to match on strings that begin with a period, contain the word "car" and then contain a semicolon before the next period.
In the past I have solved for this by splitting the range of allowable characters and putting the desired string in the middle. So for the above example the regex might be:
\..{1,35}?car.{1,35}?\.
But this solution is undesirable because I don't REALLY want to allow 75 characters between the 2 periods. I really only want to allow 50 characters. But since I don't know if the word car will occur at the very beginning or the very end, I have to find some compromise of what would be tolerable before AND after and settle on (in this example) 35 characters on both sides.
This will ultimately be in Python, but I'm hoping the principle can be explained in a flavor-neutral fashion.
People have suggested that with this simple example, I should just check the string's length afterwards.
This will not work for my needs because I want to look for multiple strings within the string. So for example I want to look for:
Some string beginning with [.;][\s"']{1,4}
and reaching a period within 150 characters but before reaching that period finding (in no particular order) at least 1 colon, CAR and a literal slash and than after that period I want to find the word PineApple within 100 characters.
In an example like this, dropping in and out of regex to check string length would be burdensome. I'm not trying to force regex to do something it can't. I'm just asking if there is a way to get it working.
(Again not looking for someone to post a regex to achieve the result above, instead looking for suggestions of how to use regex to solve the question asked before the edit).