Regexp beginner: how to remove comma before a keyword?
I have a string
"TH开发者_Go百科IS IS, A STRING, FROM"
How can I remove a comma before the FROM statement?
You may use a lookahead:
,(?=\s*FROM\b)
Or, with many keywords:
,(?=\s*(?:FROM|TO|COUNT)\b)
In Python (case insensitive):
remove_commas = re.compile(r',(?=\s*FROM\b)', re.IGNORECASE)
str = remove_commas.sub('', str)
Working example: http://ideone.com/etEnt
A Perl Example:
$text = "THIS IS, A STRING, FROM";
$text =~ s/,(\s*FROM)\b/$1/;
http://codepad.org/
Replace using regular expression \,\s*FROM
and replace with FROM
.
精彩评论