For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2006 > Regex query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regex query
Dave

2006-07-24, 6:58 pm

Hi,

I hope this is the right forum as I am doing this from perl.

I wish to use the search/replace functionality to strip out periods
from a string, but leave the first.

i.e. Change from "this.file.name.is.messy.txt" to "this file name is
messy.txt"

The file extension can be any length or any name.

So far I have tried a few things but my perl/regex knowledge is
something I am only recently building up again. I have looked through
the tutorials but nothing suitable has caught my eye.

I would like to do it in one nicely performing regex if at all
possible.

Things I have tried...

s/\.([^(txt)|^(html)])/ \1/g; # then I realised you cannot have
groupings inside a character class etc.

s/\.(\w+$){0}/ \1/g; # Well the {0} doesn't do what I was hoping for

s/\.(.+\..+)$/ \1/g; # Works if I iterate it externally enough times.

What I would love is something like s/\.(but not where \w+$ matches
here)/ /g; but I am stuck. All I want to know is if I can do this in a
regex oneliner and if so, some clues on where I should be looking :)
I'd like to work it out myself but am gonna have to ask you guys.

Cheers,

Dave

Dave

2006-07-24, 6:58 pm

> I wish to use the search/replace functionality to strip out periods
> from a string, but leave the first.

Of course, when I said first.. I meant last :)

D...

Paul Lalli

2006-07-24, 6:58 pm

Dave wrote:
> I hope this is the right forum as I am doing this from perl.
>
> I wish to use the search/replace functionality to strip out periods
> from a string, but leave the first.
>
> i.e. Change from "this.file.name.is.messy.txt" to "this file name is
> messy.txt"
>
> The file extension can be any length or any name.
>
> So far I have tried a few things but my perl/regex knowledge is
> something I am only recently building up again. I have looked through
> the tutorials but nothing suitable has caught my eye.
>
> I would like to do it in one nicely performing regex if at all
> possible.
>
> Things I have tried...
>
> s/\.([^(txt)|^(html)])/ \1/g; # then I realised you cannot have
> groupings inside a character class etc.
>
> s/\.(\w+$){0}/ \1/g; # Well the {0} doesn't do what I was hoping for
>
> s/\.(.+\..+)$/ \1/g; # Works if I iterate it externally enough times.
>
> What I would love is something like s/\.(but not where \w+$ matches
> here)/ /g;


You can. Read perldoc perlre again, and look for "lookahead
assertions"

> but I am stuck. All I want to know is if I can do this in a
> regex oneliner and if so, some clues on where I should be looking :)


Note that you can translate your pseudo code above directly, but I
would recommend instead something like
s/\.(but only if another period follows somewhere)/ /g;
instead. Again, look up the look-ahead assertions.

> I'd like to work it out myself but am gonna have to ask you guys.


If you still can't figure it out after reading about lookaheads, post
again.

Paul Lalli

Josef Moellers

2006-07-24, 6:58 pm

Dave wrote:
> Hi,
>=20
> I hope this is the right forum as I am doing this from perl.
>=20
> I wish to use the search/replace functionality to strip out periods
> from a string, but leave the first.
>=20
> i.e. Change from "this.file.name.is.messy.txt" to "this file name is
> messy.txt"
>=20
> The file extension can be any length or any name.
>=20
> So far I have tried a few things but my perl/regex knowledge is
> something I am only recently building up again. I have looked through
> the tutorials but nothing suitable has caught my eye.
>=20
> I would like to do it in one nicely performing regex if at all
> possible.
>=20
> Things I have tried...
>=20
> s/\.([^(txt)|^(html)])/ \1/g; # then I realised you cannot have
> groupings inside a character class etc.
>=20
> s/\.(\w+$){0}/ \1/g; # Well the {0} doesn't do what I was hoping for
>=20
> s/\.(.+\..+)$/ \1/g; # Works if I iterate it externally enough times.
>=20
> What I would love is something like s/\.(but not where \w+$ matches
> here)/ /g; but I am stuck. All I want to know is if I can do this in a
> regex oneliner and if so, some clues on where I should be looking :)
> I'd like to work it out myself but am gonna have to ask you guys.


I'm not sure if it can be done by a single s///. I'd do it by checking=20
for multiple .:

($s =3D~ s/\./ /) while ($s =3D~ /\..*\./);

Ex:
my $s =3D "this.file.name.is.messy.txt";
($s =3D~ s/\./ /) while ($s =3D~ /\..*\./);
print "$s\n";

Output:
this file name is messy.txt

Josef
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

it_says_BALLS_on_your forehead

2006-07-24, 6:58 pm


Dave wrote:
> Hi,
>
> I hope this is the right forum as I am doing this from perl.
>
> I wish to use the search/replace functionality to strip out periods
> from a string, but leave the first.
>
> i.e. Change from "this.file.name.is.messy.txt" to "this file name is
> messy.txt"
>
> The file extension can be any length or any name.
>
> So far I have tried a few things but my perl/regex knowledge is
> something I am only recently building up again. I have looked through
> the tutorials but nothing suitable has caught my eye.
>
> I would like to do it in one nicely performing regex if at all
> possible.
>
> Things I have tried...
>
> s/\.([^(txt)|^(html)])/ \1/g; # then I realised you cannot have
> groupings inside a character class etc.
>
> s/\.(\w+$){0}/ \1/g; # Well the {0} doesn't do what I was hoping for
>
> s/\.(.+\..+)$/ \1/g; # Works if I iterate it externally enough times.
>
> What I would love is something like s/\.(but not where \w+$ matches
> here)/ /g; but I am stuck. All I want to know is if I can do this in a
> regex oneliner and if so, some clues on where I should be looking :)
> I'd like to work it out myself but am gonna have to ask you guys.


use strict; use warnings;

my $str = 'this.file.name.is.messy.txt';
$str =~ s/\.(?=.*\.)/\ /g;
print $str, "\n";

Dave

2006-07-24, 6:58 pm

Cheers guys.

I did it with negative lookahead after going back to the docs. I just
did not know what i was looking for. Now I do :)

I will use the updated pseudocode (i.e. check for more periods rather
than a following string at the end of the line).

Ta.

anno4000@radom.zrz.tu-berlin.de

2006-07-26, 7:58 am

it_says_BALLS_on_your forehead <simon.chao@fmr.com> wrote in comp.lang.perl.misc:

> $str =~ s/\.(?=.*\.)/\ /g;


Why escape the blank?

Anno
it_says_BALLS_on_your forehead

2006-07-26, 7:58 am


anno4000@radom.zrz.tu-berlin.de wrote:
> it_says_BALLS_on_your forehead <simon.chao@fmr.com> wrote in comp.lang.perl.misc:
>
>
> Why escape the blank?


I heard that in Perl6 the /x modifier would be default for regexes, so
i'm trying to get in the habit of future proofing when i can :-).

anno4000@radom.zrz.tu-berlin.de

2006-07-26, 7:58 am

it_says_BALLS_on_your forehead <simon.chao@fmr.com> wrote in comp.lang.perl.misc:
>
> anno4000@radom.zrz.tu-berlin.de wrote:
> comp.lang.perl.misc:
>
> I heard that in Perl6 the /x modifier would be default for regexes, so
> i'm trying to get in the habit of future proofing when i can :-).


That may be, but the right hand side of a substitution isn't a regex.
I'm ready to bet you won't have to escape whitespace there, even in
Perl 6.

Anno
it_says_BALLS_on_your forehead

2006-07-26, 7:58 am


anno4000@radom.zrz.tu-berlin.de wrote:
> it_says_BALLS_on_your forehead <simon.chao@fmr.com> wrote in comp.lang.perl.misc:
>
> That may be, but the right hand side of a substitution isn't a regex.
> I'm ready to bet you won't have to escape whitespace there, even in
> Perl 6.


ahhhh, you are probably right. i always forget about the RHS...thx for
the correction!

Ted Zlatanov

2006-07-26, 6:59 pm

On 26 Jul 2006, simon.chao@fmr.com wrote:

anno4000@radom.zrz.tu-berlin.de wrote:
>
> I heard that in Perl6 the /x modifier would be default for regexes, so
> i'm trying to get in the habit of future proofing when i can :-).


I know for a fact that Perl 7 will not have regular expressions, so I
avoid using them ;)

Ted
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com