Re: Very simple regex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



----- Original Message ----- 
From: "Dotan Cohen" <dotancohen@xxxxxxxxx>
To: "For users of Fedora Core releases" <fedora-list@xxxxxxxxxx>
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


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux