简体   繁体   中英

Python text processing with regular expression

I use python 2.7.5. I have got some files in the directory/sub directory. Sample of the file1 is given below

Title file name
    path1 /path/to/file
    options path2=/path/to/file1,/path/to/file2,/path/to/file3,/path/to/file4 some_vale1 some_vale2 some_value3=abcdefg some_value4=/path/to/value some_value5

I would like to insert the text /root/directory in the text file. Final outcome i would like to have is as followes:-

Title file name
    path1 /root/directory/path/tofile
    path2=/root/directory/path/to/file1,/root/directory/path/to/file2,/root/directory/path/to/file3,/root/directory/path/to/file4
    options some_vale1 some_vale2 some_value3=abcdefg some_value4=/path/to/value some_value5 

The names path, options and path2 are same in all files. The files in the directory/subdirectory required to be modified with the same outcome as above. I tried to use the re.sub to find and replace the string. However I never got the output i wanted.

This one-liner does the entire transformation:

str = re.sub(r'(options) (\S+)', r'\2\n    \1', str.replace('/path/', '/root/directory/path/')

See a live demo of this code

You can try this:

result = re.sub(r'([ \t =,])/', replace_text, text, 1)

The last 1 is to indicate the first match only, so that only the first path is substituted.

By the way, I think that you want to conserve the space/tab or comma right? Make replace_text like this:

replace_text = r'\1/root/directory/'

Ok. Got the answer from Bohemian and Jerry. Got it working with combined code.

str = re.sub(r'(options) (\S+)', r'\2\n    \1', re.sub(r'([ \t =,])/', replace_text, text))

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