Reputation: 435
This is a basic question but I've searched a bit and am still having doubts.
Let's take the following regex as an example:
regex_example = re.compile('\d{10}\s\d-\d{3}-\d{3}-\d{4}')
Now let's imagine there is a string which may contain substrings matching the full length of that regex, such as '1234567890 1-123-456-7890' or it may just contain substrings that match the first part of the regex, say for example '1234567890 1-123'.
I would like to modify the regex (or use another solution) allowing me to match any substring that has 14 or more char and matches the regex either partially or entirely. So my code would find:
'1234567890 1-1' (has 14 characters and matches the start of the regex) or '1234567890 1-13' or '1234567890 1-134'
And so forth, up to the full length of the regex.
Upvotes: 1
Views: 137
Reputation: 785058
You can make part after first 14 characters optional:
regex_example = re.compile(
'(\d{10}\s\d-\d(?:\d{0,2}(?:(?:-\d{0,3})?(?:-\d{0,4})?)?)?)')
Upvotes: 1