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

Question:

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

Asked By: Yonbantai

||

Answers:

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

Checked this on https://regex101.com/

Answered By: Moid
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.