I want to remove parts of the string that does not match the format that I want. Example:

import re 

string = 'remove2017abcdremove'     
pattern = re.compile("((20[0-9]{2})([a-zA-Z]{4}))")
result = pattern.search(string)

if result:
    print('1')
else:
    print('0')

It returns "1" so I can find the matching format inside the string however I also want to remove the parts that says "remove" on it.

I want it to return:

desired_output = '2017abcd'

1 Answers

1
Austin On Best Solutions

You need to identify group from search result, which is done through calling a group():

import re 

string = 'remove2017abcdremove'     
pattern = re.compile("(20[0-9]{2}[a-zA-Z]{4})")
string = pattern.search(string).group()

# 2017abcd