Home > Enterprise >  how to extract these strings in Python using regex
how to extract these strings in Python using regex

Time:10-13

I have a string below:

stra = "hello (70/2009). Target no. 39/K/20/MEM/2019 world no. 12/2020 good 21/PMK.011/2010 test"

I want to extract the following substrings which contains "/" sign:

  1. 70/2009
  2. 39/K/20/MEM/2019
  3. 12/2020
  4. 21/PMK.011/2010

Can anyone advise me on how to do it?

Thank you very much!

CodePudding user response:

Not regex, but you can split then find '/' in substring like this.

stra = "hello (70/2009). Target no. 39/K/20/MEM/2019 world no. 12/2020 good 21/PMK.011/2010 test"
l = [x for x in stra.split() if '/' in x]
print(l)

CodePudding user response:

/[0-9]*[a-z]*[A-Z]*[.]*\/[0-9]*[a-z]*[A-Z]*[.]*\/*[0-9]*[a-z]*[A-Z]*[.]*\/*[0-9]*[a-z]*[A-Z]*[.]*\/*[0-9]*[a-z]*[A-Z]*[.]*

This should work but I believe there should be a better regex for this.

CodePudding user response:

using regex:

import re

text = "hello (70/2009). Target no. 39/K/20/MEM/2019 world no. 12/2020 good 
21/PMK.011/2010 test"
data = re.findall("\d \S \d ", text)
print(data)

>>>> ['70/2009', '39/K/20/MEM/2019', '12/2020', '21/PMK.011/2010']
  • Related