Very simple regex

|TF20|Shockwave shockwave at clan-tf20.com
Wed Jun 8 14:41:37 UTC 2005


----- Original Message ----- 
From: "Dotan Cohen" <dotancohen at gmail.com>
To: "For users of Fedora Core releases" <fedora-list at redhat.com>
Sent: Wednesday, June 08, 2005 9:11 AM
Subject: Very simple regex


> Now, I know that this is easy, but I am embarassed to say that I can't
> find a regex tutorial that will show me how to match any occurances of
> x and replace them with one x:
>
> xxxxx >x
> xx > x
> x > x
>
> $string="I have xxxxxxxx apples!";
> $string=preg_replace("-regex here-","x", $string);
> print "$string";
>
> I have x apples!
>

In Perl:
$string =~ s/x+/x/g;

If you are looking to do this against a text file name sample.txt:

sed -e 's/x\+/x/g' sample.txt

The trick is to use the "+" operator which matches one or more occurrences
of the preceding character.


Shockwave




More information about the fedora-list mailing list