regex - cannot find the "\" ?

G

Geoff Cox

Hello,

I am trying to replace the text between the \ and the .j - there may
be multiple instances in the line with different text between the \
and the .j

The following does not work.

if ($line =~ /\\(.*?)\.j/g ) {

$line =~ s/$1/fred/g;

}

I thought \\ would find the \ and using .*? would get the minimum
characters between a \ and the next .j but I must be wrong?

Also not sure about the use of /g

Cheers

Geoff
 
G

Geoff Cox

Hello,

I am trying to replace the text between the \ and the .j - there may
be multiple instances in the line with different text between the \
and the .j

actually not quite clear above - the text between the \ and .j is part
of the name of a file and I wish to put m- in front of the name of any
files found on each line ...

Geoff
 
B

Brian McCauley

Geoff said:
Hello,

I am trying to replace the text between the \ and the .j - there may
be multiple instances in the line with different text between the \
and the .j

The following does not work.

if ($line =~ /\\(.*?)\.j/g ) {

$line =~ s/$1/fred/g;

}

That is wrong on so many levels. You are finding part of the string
then trying to reinterpret that as another regex. You are using m//g in
a scalar context is a way that makes no sense.
I thought \\ would find the \ and using .*? would get the minimum
characters between a \ and the next .j but I must be wrong?

The ? qualifier makes .* non-greedy. That is it will match as few
characters as it can from a given starting position. It does not
however make it match as late as possible, the regex engine will still
seek the first match it can find.

You probably wanted to find every string of non-backslashes before a .j
and replace it with the string 'fred'.

s/[^\\]+\.j/fred.j/g;
 
G

Geoff Cox

You probably wanted to find every string of non-backslashes before a .j
and replace it with the string 'fred'.

s/[^\\]+\.j/fred.j/g;

Brian,

Not quite - if I follow you ....

a line might have

hello what a fine day\jane.jms\alan.jkm\james.jdm\it is getitng late

and I want to change that to

hello what a fine day\m-jane.jms\m-alan.jkm\m-james.jdm\it is getitng
late

ie put m- in front of the file names

Cheers

Geoff
 
T

Tad McClellan

Geoff Cox said:
Also not sure about the use of /g


Then read the documentation about the use of /g

If there is something in the docs that you don't understand,
then ask a question about what the docs say.
 
G

Geoff Cox

Then read the documentation about the use of /g

OK ... point taken.

What I cannot see is how to find the text whihc comes after a \ and
before the .j

\hhjkkh\gfhfhfgh\uuioui\fred.j

so I want to ignore the first 4 \ and then find the fred ...

Geoff
 
J

John W. Krahn

Geoff said:
You probably wanted to find every string of non-backslashes before a .j
and replace it with the string 'fred'.

s/[^\\]+\.j/fred.j/g;

Not quite - if I follow you ....

a line might have

hello what a fine day\jane.jms\alan.jkm\james.jdm\it is getitng late

and I want to change that to

hello what a fine day\m-jane.jms\m-alan.jkm\m-james.jdm\it is getitng
late

ie put m- in front of the file names

$ perl -le'
my $line = q/hello what a fine day\jane.jms\alan.jkm\james.jdm\it is getitng
late/;
print $line;
$line =~ s/([^\\]+\.j)/m-$1/g;
print $line;
'
hello what a fine day\jane.jms\alan.jkm\james.jdm\it is getitng late
hello what a fine day\m-jane.jms\m-alan.jkm\m-james.jdm\it is getitng late



John
 
G

Geoff Cox

$ perl -le'
my $line = q/hello what a fine day\jane.jms\alan.jkm\james.jdm\it is getitng
late/;
print $line;
$line =~ s/([^\\]+\.j)/m-$1/g;
print $line;
'

John - many thanks - works fine - can just explain a couple of points
...
how does the perl -le work ?

how does the regex work?!

it looks for one or more characters which are not \ and then .j but
how does it find the characters beteen the \ immediately to the left
of .j and .j itself ??!

Cheers

Geoff
 
F

Fabian Pilkowski

* Geoff Cox said:
John W. Krahn said:
$ perl -le'
my $line = q/hello what a fine day\jane.jms\alan.jkm\james.jdm\it is getitng
late/;
print $line;
$line =~ s/([^\\]+\.j)/m-$1/g;
print $line;
'

John - many thanks - works fine - can just explain a couple of points
..
how does the perl -le work ?

Just read `perldoc perlrun`. All those switches for your perl
interpreter are described there.
how does the regex work?!

It's almost the same regex Brian has proposed. He also explained:

| You probably wanted to find every string of non-backslashes
| before a .j and replace it with the string 'fred'.
|
| s/[^\\]+\.j/fred.j/g;

If you are not familiar with match variables like $1, $2, $3 etc you
have to read `perldoc perlre`, didn't you?

regards,
fabian
 
A

A. Sinan Unur

OK ... point taken.

What I cannot see is how to find the text whihc comes after a \ and
before the .j

\hhjkkh\gfhfhfgh\uuioui\fred.j

so I want to ignore the first 4 \ and then find the fred ...

Elsethread, you explain that this is related to filenames and such.

I am a little stumped by your approach.

Please read

perldoc File::Basename

especially the bit about fileparse.

Sinan
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top