开发者

Remove duplicate characters using a regular expression

I need to Match the second and subsequent occurances of the * character using a regular expression. I'm actual开发者_C百科ly using the Replace method to remove them so here's some examples of before and after:

test*     ->  test* (no change)
*test*    ->  *test
test** *e ->  test* e

Is it possible to do this with a regular expression? Thanks


If .NET can cope with an arbitrary amount of look behind, try replacing the following pattern with an empty string:

(?<=\*.*)\*

.

PS Home:\> 'test*','*test*','test** *e' -replace '(?<=\*.*)\*',''
test*
*test
test* e

Another way would be this pattern:

(?<=\*.{0,100})\*

where the number 100 can be replaced with the size of the target string.

And testing the following with Mono 2.0:

using System;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        Regex r = new Regex(@"(?<=\*.*)\*");
        Console.WriteLine("{0}", r.Replace("test*", ""));    
        Console.WriteLine("{0}", r.Replace("*test*", ""));    
        Console.WriteLine("{0}", r.Replace("test** *e", ""));                          
    }
}

also produced:

test*
*test
test* e


Non optimal, but another approach. Record the index of the first occurrence, replace all occurrence, and reinsert the first occurrence at the recored index.


$str =~ s/\*/MYSPECIAL/;  #only replace the first *
$str =~ s/\*//g;          #replace all *
$str =~ s/MYSPECIAL/\*/;  #put * back
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜