简体   繁体   中英

How to use replace to get rid of numbers?

I have something like:

/MyFile/14/file_1.txt 
/MyFile/17/file_2.txt 
/MyFile/10/file_3.txt

How can I use replace in regular expression? to turn them into

file 1
file 2
file 3

I've tried

.replace('/Myfile/\d+/', '').replace('_', '').replace('.txt', '')

and the output are

/MyFile/14/file 1 
/MyFile/17/file 2
/MyFile/10/file 3

Thanks in advance.

you don't need to use several replacements, you only need to use capturing groups:

import re

p = re.compile(r'^.*/(.+)_(\d+)\.txt$')
repl = r'\1 \2'
result = re.sub(p, repl, yourstring)

Note that when you write a pattern you need to use a raw string ( r'....' ) to avoid to double backslashes.

The following code would produce what you want given that the input data is a multiline string. It uses a regular expression and the sub() method of the python re module.

In the regular expression ^/MyFile/\\d+/file_(\\d+).txt$ , the parenthesis define a capturing group which can latter be used in the replacement text using \\1 (where 1 is for 1 st capturing group).

Also note the r prefix for the strings r'^/MyFile/\\d+/file_(\\d+)\\.txt$' which means python raw string and avoid us to escape the backslashes.

import re
data = """\
/MyFile/14/file_1.txt
/MyFile/17/file_2.txt
/MyFile/10/file_3.txt
"""
re_file_number = re.compile(r'^/MyFile/\d+/file_(\d+)\.txt$', re.MULTILINE)
print re_file_number.sub(r'file \1', data)

produces:

file 1
file 2
file 3

re可能会有所帮助

[ x.replace( "_", " " ) for x in re.compile(  "(?<=/MyFile/[0-9][0-9]/).+(?=.txt)" ).findall( aString ) ]

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