How to match a regex in python, either completely or partially -
How to match a regex in python, either completely or partially -
this basic question i've searched bit , still having doubts.
let's take next regex example:
regex_example = re.compile('\d{10}\s\d-\d{3}-\d{3}-\d{4}')
now let's imagine there string may contain substrings matching total length of regex, such '1234567890 1-123-456-7890' or may contain substrings match first part of regex, illustration '1234567890 1-123'.
i modify regex (or utilize solution) allowing me match substring has 14 or more char , matches regex either partially or entirely. code find:
'1234567890 1-1' (has 14 characters , matches start of regex) or '1234567890 1-13' or '1234567890 1-134'
and forth, total length of regex.
you can create part after first 14 characters optional:
regex_example = re.compile( '(\d{10}\s\d-\d(?:\d{0,2}(?:(?:-\d{0,3})?(?:-\d{0,4})?)?)?)')
regex demo
python regex
Comments
Post a Comment