English 中文(简体)
如何消除额外的双重引用?
原标题:How to remove the extra double quote?
  • 时间:2011-10-25 09:40:08
  •  标签:
  • ruby
  • regex

In a malformed .csv file, there is a row of data with extra double quotes, e.g. the last line:

Name,Comment
"Peter","Nice singer"
"Paul","Love "folk" songs"

How can I remove the double quotes around folk and replace the string as:

Name,Comment
"Peter","Nice singer"
"Paul","Love _folk_ songs"
问题回答

在Rub.1.9,以下著作:

result = subject.gsub(/(?<!^|,)"(?!,|$)/,  _ )

Previous versions don t have lookbehind assertions.

<>Explanation:

(?<!^|,)  # Assert that we re not at the start of the line or right after a comma
"         # Match a quote
(?!,|$)   # Assert that we re not at the end of the line or right before a comma

当然,这假定,我们赢得了像病理学一样的 t。

"Mary",""Oh," she said"

如果你没有在Rub.1.9上台,或者仅仅退休,有时会把座标分为<代码>,,删除第一种/最后报价,将剩余的<代码><>><>>>>/代码改为_s,re-quote,并加入,

页: 1 必须担心效率!

$str =  "folk" ;

$new = str_replace( " ,   , $str);

/* now $new is only folk, without " */

元战略:

It s likely the case that the data was manually entered inconsistently, CSV s get messy when people manually enter either field terminators (double quote) or separators (comma) into the field itself. If you can have the file regenerated, ask them to use an extremely unlikely field begin/end marker, like 5 tilde s (~~~~~), and then you can split on "~~~~~,~~~~~" and get the correct number of fields every time.

除非你别无选择,否则,档案就会以正确的方式重新生成。 任何其他做法都要求引起麻烦,因为插入令人不安的替罪状是损失的,因此无法得到可靠的逆转。

如果你能够从来文方那里找到档案,那么Tim Pietzcker的reg子比任何东西都好,但我强烈建议你用你的文字打印所有“固定”线,并用人工核对错误。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签