Regular Expression Query

L

lonelyplanet999

Hi,

I'm studying perl programming now. Previously I encountered a script
with 3 statements as below.

# space between a +, - or " char b4 a keyword? remove it!
$in{keywords}=~s/ \+ +/ \+/g;
$in{keywords}=~s/ \- +/ \-/g;
$in{keywords}=~s/ \" +/ \"/g;

I would like to ask how the regexps function as the comment describe ?

Thanks!
 
N

nobull

Subject: Regular Expression Query

Poor subject line.

See http://www.cpan.org/authors/id/D/DM/DMR/subjects.post
# space between a +, - or " char b4 a keyword? remove it!
$in{keywords}=~s/ \+ +/ \+/g;
$in{keywords}=~s/ \- +/ \-/g;
$in{keywords}=~s/ \" +/ \"/g;

I would like to ask how the regexps function as the comment describe ?

They do not. The contstraint "before a keyword" is not in there.

Lets just look at one:

s/ \- +/ \-/g;

The backslashes are redundant, hyphen is not meta in string or regex.

s/search/relpace/g means replace as many non-overlapping matches for
the pattern /search/ with the string replace.

The pattern / - +/ matches a space, followed by a hyphen followed by
one or more spaces.

The string " -" is, err..., the string " -".

BTW the =~ binding operator is used to specify the variable on which
s/// is to act.

That's all there is to it.

No capturing, no interpolation, no subtlties. This really is about
the simplest s/// example you could imagine.

If you are at the stage where you have many questions at this level
then you probably are not at the stage where asking questions in
newsgroups is an effective use of your time or other people's. You
should probably be working through some basic Perl tutorials.

Actually, assuming this is someone else's code, it would appear that
the person who wrote it is not much further advanced. Anyone who had
even basic competance in Perl would usually combine these into a
single s///g

s/( [-+"]) +/$1/g;

As I said before attempting to learn Perl programming from this code
is not a good idea.

I have already pointed out to you that this newsgroup does not exist.
Really, it doesn't (it only appears to exist on misconfigured news
servers). For further explaination please see the FAQ. Please do not
start any more threads here.
 

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