Regular expressions

  • Thread starter Jørn Dahl-Stamnes
  • Start date
J

Jørn Dahl-Stamnes

I'm trying to extract parts of a string with the following line:

$relay =~ s/^\[(.+)\]$|^(.+) \[.+$/$1/;

But $relay contain data only if it match the first part of the expression.
I have tested both parts of the expression, and they both work.

Can the |-operator be used in the line above?
 
P

Paul Lalli

Jørn Dahl-Stamnes said:
I'm trying to extract parts of a string with the following line:

$relay =~ s/^\[(.+)\]$|^(.+) \[.+$/$1/;

But $relay contain data only if it match the first part of the expression.
I have tested both parts of the expression, and they both work.

Can the |-operator be used in the line above?

The | can be used there, yes. The $1, however, cannot. $1 will ALWAYS
refer to the first parentheses in the entire match, regardless of
whether or not the sub-pattern that contains it was actually matched.
You want the little known $+ variable instead. It's documented in

perldoc perlvar

$+ The text matched by the last bracket of the last
successful search pattern. This is useful if you
don't know which one of a set of alternative
patterns matched. For example:

/Version: (.*)|Revision: (.*)/ && ($rev = $+);


Paul Lalli
 
J

Jørn Dahl-Stamnes

Paul said:
Jørn Dahl-Stamnes said:
I'm trying to extract parts of a string with the following line:

$relay =~ s/^\[(.+)\]$|^(.+) \[.+$/$1/;

But $relay contain data only if it match the first part of the
expression. I have tested both parts of the expression, and they both
work.

Can the |-operator be used in the line above?

The | can be used there, yes. The $1, however, cannot. $1 will ALWAYS
refer to the first parentheses in the entire match, regardless of
whether or not the sub-pattern that contains it was actually matched.
You want the little known $+ variable instead. It's documented in

perldoc perlvar

Thanks, it worked. I could not find this in the O'Reillys "Programming Perl"
book I got. But then, I have not read every word in it.
 
P

Paul Lalli

Jørn Dahl-Stamnes said:
Thanks, it worked. I could not find this in the O'Reillys "Programming Perl"
book I got. But then, I have not read every word in it.

Page 184 in the Regexp section on Caputring and Clustering, and page
668 in the list of all special variables. In the 3rd edition, at
least. . .

Paul Lalli
 
J

Jørn Dahl-Stamnes

Paul said:
Page 184 in the Regexp section on Caputring and Clustering, and page
668 in the list of all special variables. In the 3rd edition, at
least. . .

I only got the 2nd edition... :-(
And it does not have any "Update book" function... guess I must buy a new
version.
 
P

Paul Lalli

Jørn Dahl-Stamnes said:
I only got the 2nd edition... :-(
And it does not have any "Update book" function... guess I must buy a new
version.

You should *really* *really* consider doing so. Perl has changed quite
a bit since version 5.0 was released, which is what the 2nd ed of the
Camel covers.

Paul Lalli
 
B

Ben Morrow

Quoth "Paul Lalli said:
You should *really* *really* consider doing so. Perl has changed quite
a bit since version 5.0 was released, which is what the 2nd ed of the
Camel covers.

OTOH, Perl has also changed quite a bit since 5.6.0, which is what the
Blue Camel covers. While I learnt masses about Perl from reading the
Camel through, I don't find I refer to it at all now: the perldocs are
more useful, and always up to date.

My suggestion would be to get a copy of the 3rd ed. from a library and
read it through, to get some idea of what has changed. Note that in
particular all of the 'upcoming' Unicode stuff and all of the IO layers
stuff has changed quite significantly since 5.8.

Ben
 
J

John W. Krahn

Ben said:
OTOH, Perl has also changed quite a bit since 5.6.0, which is what the
Blue Camel covers.

Both the 2nd and 3rd editions have blue covers.


John
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top