last day (16 days later) » 

5:20 PM
1
A: What would be the regex pattern for the following?

The fourth birdYou could use 3 capture groups: \b(AB)\D*(\d+)\S*?(?:20)?(\d\d)\b \b A word boundary to prevent a partial word match (AB) Capture AB in group 1 \D* Match optional non digits (\d+) Capture 1+ digits in group 2 \S*? Optionally match non whitespace characters, as least as possible (?:20)? Optional...

 
(re.sub(pattern, r"\1/\2/\3", s)) What does this line do? And can you check I have edited my post if this will work for that too?
 
@prathambhatia I have updated the answer getting all the matches. Do you want those, or do you still only want 4 matches A,B,C,D as in the expected output in your question?
 
I will try this in my orignal dataset with 300,000+ rows and let you know if it works. Can you explain me what does \S*?(?:20)? line exactly does? Why have you added \S* and then a question mark and also (?:20) question mark inside the group what does that do?
 
@prathambhatia I have added a breakdown of the pattern for the separate parts.
 
Can you explain in detail how does that line exactly work I'm new to coding. Would be a great learning for me. \S*?(?:20)? This line. Question mark inside group and then a :(colon)
This works absolutely fine for my dataset.
can you tell me what would be the regex pattern in case if there are multiple numbers in between along with all the other patterns of string mentioned above. Example :-AB-231-2-33-12-2017,AB-231-1-HD-2017,AB-231-2017 where I want to extract AB/231/2017. Your code works fine for the examples that I mentioned above but what if I want to include these examples as well?
 
5:20 PM
@prathambhatia Like this? \b(AB)\D*(\d+)\S*?(?:20)?(\d\d)\b(?=[',]|-[A-Z]|$) regex101.com/r/78pWhT/1
 
will this work if I have alphabets too in between? example: IA-12AB-231-2-33-RR-TTLT-ZA-2017IA-FM-22, where i want AB/231/2017
I checked the one that you mentioned right now it works fine for digits but not alphabets. Apologies for not giving all the examples together.
AB/231/17 works too IA-12AB-231-2-33-RR-TTLT-ZA-2017IA-FM-22 for this pattern mentioned above. Would really appreciate if you could solve this.
Or just consider AB-231-2-33-RR-TTLT-ZA-2017 pattern if its not possible to do the above one because of word boundary. Output expected : AB-231-17
 
These extra cases can make the pattern more complex and there should be some rules or logic that separates one match from another. \b(AB)\D*(\d+)\S*?(\d\d)\b(?=[',]|-[A-Z]|$)(?!-[A-Z]+-[A-Z]+‌​-[A-Z]) regex101.com/r/zoe5Yh/1
 
I'll try this in my dataset. And I have edited question as well for better understanding
 
@prathambhatia If you change the question, you render the given answers invalid.
 
Yes my bad,I should have mentioned all the patterns together
Could you please try to solve regex for all the patterns that I mentioned in question ?
 
5:25 PM
@prathambhatia Like this (AB)\D*(\d+)\S*?(\d\d)\b(?=\s|[,'/]|-[A-Z]{2}\b) regex101.com/r/k1uc0A/1
 
5:49 PM
I tried this in for loop for exact examples that I mentioned in ques using re.search. I'm not getting output for some
sss=['AB.224-QW-2018','AB.876-5-LS-2018','AB.26-LS-18','AB-123-6-LS-2017','IA-Mb-22L-AB.224-QW-2018-IA-Mb-22L','ZX-ss-12L-AB-123-6-LS-2017-BC-22','AB.224-2018','AB.224/QW/2018','AB/224/2018','AB-10-HDB-231-NCLT-1-2017 AD-42-HH-2019','AB-1-HDB-NCLT-1-2016 AD-42-HH-2020','AB-1-HDB-NCLT-1-2016/(AD-42-HH-2020)']



for i in sss:
if re.search(r"(AB)\D*(\d+)\S*?(\d\d)\b(?=\s|[,'/]|-[A-Z]{2}\b)",i)==None:
print('no')
else:
d=re.search(r"(AB)\D*(\d+)\S*?(\d\d)\b(?=\s|[,'/]|-[A-Z]{2}\b)",i)
b=d.group(1)+"/"+d.group(2)+"/"+d.group(3)
 
@prathambhatia I am offline for a few hours, I will take a look when I am back again.
 
Sure no worries, thanks a lot
 
 
3 hours later…
8:46 PM
@prathambhatia I forgot an alternative for the end of the string. See a Python demo here ideone.com/JUUi5d and the regex (AB)\D*(\d+)\S*?(\d\d)\b(?=\s|[,'/]|-[A-Z]{2}\b|$) regex101.com/r/7eaoXt/1
 

  last day (16 days later) »