I am currently new to Regular Expressions and would appreciate if someone can guide me through this.
import re
some = "I cannot take this B01234-56-K-9870 to the house of cards"
I have the above string and trying to extract the string with dashes (B01234-56-K-9870) using python regular expression. I have following code so far:
regex = r'\w+-\w+-\w+-\w+'
match = re.search(regex, some)
print(match.group()) #returns B01234-56-K-9870
Is there any simpler way to extract the dash pattern using regular expression? For now, I do not care about the order or anything. I just wanted it to extract string with dashes.
\w+(?:-\w+)+would do it. If you expect exactly 3 dashes then\w+(?:-\w+){3}\wmatches the_as well. Personally, I would find out if there is a more identifiable pattern and use that, such as(?:[A-Z\d]+-){3}\d+if the final group is always digits and the first three groups are all caps and digits.