简体   繁体   中英

I need to extract only particular pattern from a string using python

As i need to extract only particular pattern from string:

import re

string='/x/eng/wcov/Job148666--rollup_generic/Job148674--ncov_aggregate/Job148678--run_command/Job148678.info:  devN_180107_2035'

line2=re.findall(r'(?:/\w*)' ,string)
print(line2)

I'm getting output as below:

['/x', '/eng', '/wcov', '/Job148666', '/Job148674', '/Job148678', '/Job148678']

But actual output i required is:

/x/eng/wcov/Job148666--rollup_generic/Job148674--ncov_aggregate/Job148678--run_command/Job148678.info

Try using split() function

string='/x/eng/wcov/Job148666--rollup_generic/Job148674--ncov_aggregate/Job148678--run_command/Job148678.info:  devN_180107_2035'

sp=string.split(':')[0]

Does the string always end with : ? Then use this

str.split(":", 1)[0]

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