开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜