Regex Help

R

rmurthy60

I tried doing the following to remove the ./ from the file listed
below. I am able to do it in sed but the problem is you cannot use
qx'sed -e"s?\([ /]\)\./?\1?g" $filist'. It does not allow the use of
$filist, but if I hard code the file name in place of $filist. It
works.

next if s?\([ /]\)\./?\1?g;

For some reason it is not removing the ./ from the file. Any
suggestions are
welcome.
The file is in this format

a b ./dsfj/dfl/dksl ./ksdfl/dsld

c d ./sds/dsl/dksld ./kdf/ksd/ksdk

Thanks

Raghu
 
G

Greg Bacon

: I tried doing the following to remove the ./ from the file listed
: below. I am able to do it in sed but the problem is you cannot use
: qx'sed -e"s?\([ /]\)\./?\1?g" $filist'. It does not allow the use of
: $filist, but if I hard code the file name in place of $filist. It
: works.

$ cat try
#! /usr/local/bin/perl

use warnings;
use strict;

while (<DATA>) {
# delete the /g if you only want the first hit
s!(^|\s+)\./!$1!g;
print;
}

__DATA__
a b ./dsfj/dfl/dksl ./ksdfl/dsld
c d ./sds/dsl/dksld ./kdf/ksd/ksdk
./test

$ ./try
a b dsfj/dfl/dksl ksdfl/dsld
c d sds/dsl/dksld kdf/ksd/ksdk
test

Hope this helps,
Greg
 
R

rmurthy60

Thanks a lot. That worked.

Bernard El-Hagin said:
(e-mail address removed) wrote in @posting.google.com:
I tried doing the following to remove the ./ from the file listed
below. I am able to do it in sed but the problem is you cannot use
qx'sed -e"s?\([ /]\)\./?\1?g" $filist'. It does not allow the use of
$filist, but if I hard code the file name in place of $filist. It
works.

next if s?\([ /]\)\./?\1?g;

For some reason it is not removing the ./ from the file. Any
suggestions are
welcome.
The file is in this format

a b ./dsfj/dfl/dksl ./ksdfl/dsld

c d ./sds/dsl/dksld ./kdf/ksd/ksdk


I'm not sure I understand, but why not simply:


s#\./##g;


?


Cheers,
Bernard
 
E

Eric Bohlman

(e-mail address removed) wrote in
next if s?\([ /]\)\./?\1?g;

You're forgetting that in Perl, unlike sed, unescaped parentheses are
capture/grouping metacharacters and escaped parentheses match literal
parentheses. Your regex doesn't capture anything.

Whoever came up with the idea of using escaped parentheses as
metacharacters should be required to read Gerald Weinberg's _The Psychology
of Computer Programming_, particularly the section on "Programming as an
Individual Activity." It works against, rather than with, the mind's
ability to generalize. "Everything matches literally expect for these
metacharacters: ..., and you can force any of them to be treated literally
by escaping them" is a nice general rule (Larry Wall obviously felt that
way). Making escaping behave one way for every metacharacter except
parentheses and the exact opposite way for parentheses simply increases the
cognitive load of writing a regex.
 

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

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,364
Latest member
Stevanida

Latest Threads

Top