简体   繁体   中英

Python Regex: Remove optional characters

I have a regex pattern with optional characters however at the output I want to remove those optional characters. Example:

string = 'a2017a12a'    
pattern =  re.compile("((20[0-9]{2})(.?)(0[1-9]|1[0-2]))")
result = pattern.search(string)
print(result)

I can have a match like this but what I want as an output is:

desired output = '201712'

Thank you.

You've already captured the intended data in groups and now you can use re.sub to replace the whole match with just contents of group1 and group2.

Try your modified Python code,

import re

string = 'a2017a12a'    
pattern =  re.compile(".*(20[0-9]{2}).?(0[1-9]|1[0-2]).*")
result = re.sub(pattern, r'\1\2', string)
print(result)

Notice, how I've added .* around the pattern, so any of the extra characters around your data is matched and gets removed. Also, removed extra parenthesis that were not needed. This will also work with strings where you may have other digits surrounding that text like this hello123 a2017a12a some other 99 numbers

Output,

201712

Regex Demo

You can just use re.sub with the pattern \\D (=not a number):

>>> import re
>>> string = 'a2017a12a'
>>> re.sub(r'\D', '', string)
'201712'

Try this one:

import re
string = 'a2017a12a'    
pattern =  re.findall("(\d+)", string)  # this regex will capture only digit
print("".join(p for p in pattern))  # combine all digits

Output:

201712

If you want to remove all character from string then you can do this

import re
string = 'a2017a12a'
re.sub('[A-Za-z]+','',string)

Output:

'201712'

You can use re module method to get required output, like:

    import re

    #method 1
    string = 'a2017a12a'
    print (re.sub(r'\D', '', string))

    #method 2
    pattern =  re.findall("(\d+)", string)
    print("".join(p for p in pattern))

You can also refer below doc for further knowledge.

https://docs.python.org/3/library/re.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM