Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Regex Conundrum

by SciDude (Friar)
on Jun 12, 2004 at 19:38 UTC ( #363646=note: print w/ replies, xml ) Need Help??

in reply to Regex Conundrum

There is a standard solution to this problem, mostly from Mastering Regular Expressions:

# You need to match a double quoted string with the following regex # [^"\\]*(\\.[^"\\]*)*",? # # But to get the text between double quotes use some ( ) # ([^"\\]*(\\.[^"\\]*)*)",? # gets text inside quotes as $1 # # but you also have non quoted fields, thus # ([^,]+),? # which should match things optionally followed by a comma # # and then a match for separation commas # , # # this must be repeated with m/.../g

Before attempting this yourself, take at look at Text::ParseWords and the quotewords routine. This should solve your problem. If the module is not available to you then the following untested code from Mastering Regular Expressions should work:

@fields = (); while ($text =~ m/"([^"\\]*(\\.[^"\\]*)*)",?|([^,]+),?|,/g { push (@fields, defined ($1) ? $1 : $3) ; } push (@fields, undef) if $text =~ m/,$/; # Account for the special cas +e of an empty last field. # all data is now in @fields

Note: untested.

The first dog barks... all other dogs bark at the first dog.

Comment on Re: Regex Conundrum
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://363646]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2015-11-25 07:34 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (672 votes), past polls