简体   繁体   中英

Regex to get all occurrences of a pattern followed by a value in a comma separate string

This is in python

Input string :

Str = 'Y=DAT,X=ZANG,FU=_COG-GAB-CANE-,FU=FARE,T=TART,RO=TOP,FU=@-_MAP.com-,Z=TRY'

Expected output

'FU=_COG-GAB-CANE_,FU=FARE,FU=@-_MAP.com_'

here ' FU= ' is the occurence we are looking for and the value which follows FU=

return all occurrences of FU= (with the associated value for FU=) in a comma-separated string, they can occur anywhere within the string and special characters are allowed.

Here is one approach.

>>> import re
>>> str_ = 'Y=DAT,X=ZANG,FU=FAT,T=TART,FU=GEM,RO=TOP,FU=MAP,Z=TRY'
>>> re.findall.__doc__[:58]
'Return a list of all non-overlapping matches in the string'
>>> re.findall(r'FU=\w+', str_)
['FU=FAT', 'FU=GEM', 'FU=MAP']
>>> ','.join(re.findall(r'FU=\w+', str_))
'FU=FAT,FU=GEM,FU=MAP'

Got it working

Python Code

import re

str_ = 'Y=DAT,X=ZANG,FU=_COG-GAB-CANE-,FU=FARE,T=TART,RO=TOP,FU=@-_MAP.com-,Z=TRY'

str2='FU='+',FU='.join(re.findall(r'FU=(.*?),', str_))

print(str2)

Gives the desired output :

'FU=_COG-GAB-CANE-,FU=FARE,FU=@-_MAP.com-'

Successfully gives me all the occurrences of FU= followed by values, irrespective of order and number of special characters.

Although a bit unclean way as I am manually adding FU= for the first occurrence. Please suggest if there is a cleaner way of doing it ? , but yes it gets the work done.

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