Search code examples
pythonpython-3.xregexpython-re

How to use regex in python to catch patterns like keyword1 [arbitrary characters with a max length of 5] keyword2


How can I use re package to recognize a pattern as

keyword1 [arbitrary characters with a max length of 5] keyword2

For example, keyword1 abc keyword2 is valid, keyword1 a . keyword2 is also valid but keyword1 1234abd keyword2 is not valid

Thank you


Solution

  • r"(?:keyword1).{5}(?:keyword2)
    

    Checked this on https://regex101.com/