user3153140
user3153140

Reputation: 59

Delete string in between special characters in python

I have string something like this

mystring = "CBS Network Radio Panel;\ntitle2 New York OCT13W4, Panel Weighting;\n*options; mprint ls=max mprint;\n\n****************************************out; asd; ***hg;"

I want to delete the string between * and ; output should be

"CBS Network Radio Panel;\ntitle2 New York OCT13W4, Panel Weighting;\ mprint ls=max mprint;\n\n asd;"

I have tried this code

re.sub(r'[\*]*[a-z]*;', '', mystring)

But it's not working.

Upvotes: 1

Views: 75

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

You may use

re.sub(r'\*[^;]*;', '', mystring)

See the Python demo:

import re
mystring = "CBS Network Radio Panel;\ntitle2 New York OCT13W4, Panel Weighting;\n*options; mprint ls=max mprint;\n\n****************************************out; asd; ***hg;"
r = re.sub(r'\*[^;]*;', '', mystring)
print(r)

Output:

CBS Network Radio Panel;
title2 New York OCT13W4, Panel Weighting;
 mprint ls=max mprint;

 asd;

The r'\*[^;]*;' pattern matches a literal *, followed with zero or more characters other than ; and then a ;.

Upvotes: 3

Related Questions