Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular Expression newbie help/question

Reply
Thread Tools

Regular Expression newbie help/question

 
 
Sven
Guest
Posts: n/a
 
      10-23-2003
Hi,
I need some assistance on a search and replace issue with a
commaseparated file.
I need to replace all commas(,) on every lines EXCEPT those that occur
within doublequotes (",")

Thanks,

Sven
 
Reply With Quote
 
 
 
 
Greg Bacon
Guest
Posts: n/a
 
      10-23-2003
In article <> ,
Sven <> wrote:

: I need some assistance on a search and replace issue with a
: commaseparated file.
: I need to replace all commas(,) on every lines EXCEPT those that occur
: within doublequotes (",")

If you want to roll your own:

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

use warnings;
use strict;

while (<DATA>) {
s/(".*?"|,)/$1 eq ',' ? '<=>' : $1/eg;
print;
}

__DATA__
"foo,bar"
foo,bar
foo,bar,"baz,quux"
foo,,bar,"baz,quux"
foo,bar,,"baz,,quux"

$ ./try
"foo,bar"
foo<=>bar
foo<=>bar<=>"baz,quux"
foo<=><=>bar<=>"baz,quux"
foo<=>bar<=><=>"baz,,quux"

Consider using Text::CSV_XS, available on the CPAN.

Greg
--
The greatest dangers to liberty lurk in insidious encroachment by men
of zeal, well-meaning but without understanding.
-- Justice Louis D. Brandeis
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      10-23-2003
Sven <> wrote:

> I need to replace all commas(,) on every lines EXCEPT those that occur

^^^^^^
> within doublequotes (",")



perldoc -q EXCEPT

How can I split a [character] delimited string except when inside
[character]? (Comma-separated files)


You are expected to check the Perl FAQ before posting to the
Perl newsgroup.

After you get the fields split, use join() to put them back
together with some other separator.

Or, better yet, use a module that understands the CSV format.

http://search.cpan.org/search?query=CSV&mode=all


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-23-2003
Sven wrote:
> I need some assistance on a search and replace issue with a
> commaseparated file.


> I need to replace all commas(,) on every lines EXCEPT those that
> occur within doublequotes (",")


s/("[^"]*")|,/$1 ? $1 : $replacement/eg;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-24-2003
(Sven) wrote in news:ad7279fb.0310231202.3fcc779
@posting.google.com:

> Hi,
> I need some assistance on a search and replace issue with a
> commaseparated file.
> I need to replace all commas(,) on every lines EXCEPT those that occur
> within doublequotes (",")


Please read the FAQ before posting. Thanks.

--
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
 
Reply With Quote
 
Helgi Briem
Guest
Posts: n/a
 
      10-24-2003
On Fri, 24 Oct 2003 08:46:37 -0500, "Eric J. Roode"
<> wrote:

> (Sven) wrote in news:ad7279fb.0310231202.3fcc779
>@posting.google.com:
>
>> Hi,
>> I need some assistance on a search and replace issue with a
>> commaseparated file.
>> I need to replace all commas(,) on every lines EXCEPT those that occur
>> within doublequotes (",")

>
>Please read the FAQ before posting. Thanks.


While I will usually be the first one to recommend
reading the documentation, the least you could do
is point the OP to the relevant bit of documentation
as it is far from easy to find.

"How can I split a [character] delimited string except
when inside [character]?"

The relevant section is in perlfaq4 and can be read
either by browsing through that document:

perldoc perlfaq4

or, searching for it within the perlfaq documents:

perldoc -q split

or

perldoc -q delimited

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments