0

Can we use anything else except def in the pattern

import re pattern = r"(?P<first>abc)(?:def)(ghi)" match = re.match(pattern, "abcdefghi") if match: print(match.group("first")) print(match.groups())

26th Jul 2018, 3:03 PM
adarsh pandey
adarsh pandey - avatar
1 Réponse
0
If your question is whether you can put other strings in the pattern in place of the characters 'def' and still have it match the text and return the same groups, there are an infinite number of options. Here are a few to get you thinking along these lines. pattern = r"(P<first>abc)(?:.+)(ghi)" pattern = r"(P<first>abc)(?:\w{3})(ghi)" pattern = r"(P<first>abc)(?:[a-z]*)(ghi)" pattern = r"(P<first>abc)(?:[d-f]{1,3})(ghi)" As you can imagine, you can play with the patterns in an unlimited number of ways and combinations depending on your needs and the other strings you want to match or avoid matching.
30th Jul 2018, 12:53 AM
1_coder