s// problem

W

W. Bauer

Hello

I don't get my first s// working: I would like to replace spaces by ; but
only if the space is not within double quotes where some can be between the
space and the quotes. Example:

"abc def" a -->"abc def";a

[^"][^a-z]+[\s][^a-z]+[^"]
matches these spaces correctly. But
s/[^"][^a-z]+[\s][^a-z]+[^"]/;/
does not work properly. Thanks for any hint,

W. Bauer
 
G

Gunnar Hjalmarsson

W. Bauer said:
I don't get my first s// working: I would like to replace spaces by
; but only if the space is not within double quotes where some can
be between the space and the quotes.

You didn't exactly choose a simple first s/// task. :)
Example:

"abc def" a -->"abc def";a

[^"][^a-z]+[\s][^a-z]+[^"]
matches these spaces correctly.

How did you reach that conclusion? (It doesn't.)

I think this does what you want:

s/("[^"]*")|\s+/ $1 || ';' /eg;

Please study "perldoc perlre" and "perldoc perlop" more carefully to
get an understanding of what it does.

It should be noted that there also are modules for handling this kind
of replacement. Text::parseWords is worth exploring, for instance.
 
W

W. Bauer

Gunnar Hjalmarsson said:
Example:

"abc def" a -->"abc def";a

[^"][^a-z]+[\s][^a-z]+[^"]
matches these spaces correctly.

I was so desperate after two hours of trying regexp, I must have copied
something wrong.

I think this does what you want:

s/("[^"]*")|\s+/ $1 || ';' /eg;

GREAT, it does exactly what I needed.
Please study "perldoc perlre" and "perldoc perlop" more carefully to
get an understanding of what it does.

It should be noted that there also are modules for handling this kind
of replacement. Text::parseWords is worth exploring, for instance.

That's a very valuable hint. Would you have also a hint how to find out
about which modules are available? As a newbie, I basically stick to the
tutorials and references on perldoc, but I haven't seen any overview of
the modules available.

Best regards, W. Bauer
 
T

Tore Aursand

Would you have also a hint how to find out about which modules are
available? As a newbie, I basically stick to the tutorials and
references on perldoc, but I haven't seen any overview of the modules
available.

Depending on what you mean by "available", actually. For a list of
modules that come with your Perl distribution:

perldoc perlmodlib

Finding out which modules are installed on your system is a FAQ:

perldoc -q installed

For finding, and installing, new modules, visit CPAN:

http://www.cpan.org/

If you're running Linux, you can launch the CPAN interface from the
command line:

perl -MCPAN -e shell
 
B

Ben Morrow

Tore Aursand said:
If you're running Linux, you can launch the CPAN interface from the
command line:

perl -MCPAN -e shell

s/Linux/an OS with a command line/

There are more than a few modules which require a C compiler, though,
so if you're on Win32 you'd be better off using ppm (invoked as such
from the command line) which will install pre-built binary versions.
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top