开发者

Regexp beginner: how to remove comma before a keyword?

开发者 https://www.devze.com 2023-03-03 13:24 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消