Regex query

D

Dave

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
 
J

Josef Moellers

Dave said:
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.

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

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

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

Output:
this file name is messy.txt

Josef
 
P

Paul Lalli

Dave said:
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
 
I

it_says_BALLS_on_your forehead

Dave said:
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";
 
D

Dave

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.
 
I

it_says_BALLS_on_your forehead

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 :).
 
A

anno4000

it_says_BALLS_on_your forehead said:
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
 
I

it_says_BALLS_on_your forehead

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!
 
T

Ted Zlatanov

On 26 Jul 2006, (e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top