How to match a specific character only if followed by a string containing specific characters

I am trying to replace slashes in a string, but not all of them are only those that are before the first comma. To do this, I probably should find a way to match only slashes, followed by a line containing a comma.

Is it possible to do this using one regexp, i.e. without first breaking the line with commas?

Example input line:

Abc1/Def2/Ghi3,/Dore1/Mifa2/Solla3,Sido4

What I want to get:

Abc1.Def2.Ghi3,/Dore1/Mifa2/Solla3,Sido4

I tried some lookahead and lookbehind methods without effect, so this is currently being done, for example, Python, I split the data first:

test = 'Abc1/Def2/Ghi3,/Dore1/Mifa2/Solla3,Sido4'
strlist = re.split(r',', test)
result = ','.join([re.sub(r'\/', r'.', strlist[0])] + strlist[1:])

regexp Python- , pattern replacement, :

result = re.sub(pattern, replacement, test)

, , - , , (, sed Python).

+4
2
item = 'Abc1/Def2/Ghi3,/Dore1/Mifa2/Solla3,Sido4'
print item.replace("/", ".", item.count("/", 0, item.index(",")))

, . , , .

+3

lookbehind, , . re .

s = 'Abc1/Def2/Ghi3,/Dore1/Mifa2/Solla3,Sido4'

left,sep,right = s.partition(',')

sep.join((left.replace('/','.'),right))
Out[24]: 'Abc1.Def2.Ghi3,/Dore1/Mifa2/Solla3,Sido4'
+1

All Articles