I am using python to perform some text analysis and using regex to get required pattern from the text.
I would like to know, how can i restrict a pattern to specified number of characters.
For Eg - We have make use of {a,b} to define lower and upper bound of a specified character. How can the same logic be applied to a full pattern.
I have the following code:
import re
text = '''
547 U18101MH1988PTCO50073 =RAINBOW GARMENTS AND EXPORTS (INDIA) PRI
548 UU24230MH2004PTC147574 RAINBOW HERBAL AND RESEARCH PRIVATE LIMI
549 U55200MH1992PTCO067327 RAINBOW HOLIDAY-INNPVT.LTD.
550 U15100MH2004PTC148619 RAINBOW HORTICULTURE PRIVATE LIMITED
551 US9S99S9MH2000PTC124660 RAINBOW INFOTECH (INDIA) PRIVATE LIMITED
552 U70200MH2011PTC221779 RAINBOW INFRAVENTURES PRIVATE LIMITED
553 U15130MH2004P1C14/013 RAINBOW JELSTAR FOODS PRIVATE LIMITED
'''
re.findall('U.*\d{5}.*\d',text)
This output would give me a list of values from the text. I want to restrict the length of individual values in the list to be between 19 to 21. How can i achieve the result?
Also, out of curiosity, is it possible to get all possible matches in a given text - for eg - UU24230MH2004PTC147574 value can have multiple multiple match possible using the string provided along with the restriction of length as mentioned above?
I know i can achieve some bit of it using list comprehension but want regex to do it for me as i have complex regex query string and this is just a small component of the overall.
Thank you all