-1

I am dealing with csv file and i am converting it to DataTable. But it contains strings as follows.

"Pull PEG Kit, 24" , Ref:P-PEG-24, Exp: 2019/03"

Above phrase is one word. i have used following Regular expression to obtain values within double quotes.

"\".*?\"" 

But in this scenario it break the word as

"Pull PEG Kit, 24"

Ref:P-PEG-24

Exp: 2019/03"

but i need the word

Pull PEG Kit, 24" , Ref:P-PEG-24, Exp: 2019/03

how can i solve this using c#? please help me to solve this

Thanks in advance.

Community
  • 1
  • 1
BonieZat
  • 163
  • 3
  • 14
  • How can you define the context where `"` should be considered part of the single field? If you cannot, regex won't help. – Wiktor Stribiżew Mar 29 '17 at 08:09
  • why don't you just use string's `Trim('"')` method? – Nino Mar 29 '17 at 08:17
  • 2
    Note that the csv is invalid at the moment, a double-quote in the value `"` should be escaped like that: `""`. Also why using regex if there are csv parser available? – meica Mar 29 '17 at 08:21
  • double quotes in the value means 24 inches.. What is csv parser? can you provide me a reference? – BonieZat Mar 29 '17 at 09:15
  • @BonieZat Yeah, but what I meant is a text like `te" xt` saved as csv should result in `"te"" xt"` in order to [escape the double quotes](http://stackoverflow.com/a/17808731/4465512). One way to read csv files is shown in [this answer](http://stackoverflow.com/a/3508572/4465512) – meica Mar 29 '17 at 11:28
  • The CSV string is invalid. How can a *CSV* pattern know to *not stop* at an internal quote and comma? It appears that you have a CSV with internal CSVs. – ΩmegaMan Apr 10 '17 at 01:03

1 Answers1

0

I might be missing something out here, but the most basic solution yields the reuslt you need:

var regex = new Regex("(.*)");
var str = "Pull PEG Kit, 24\", Ref:P - PEG - 24, Exp: 2019 / 03";
var result = regex.Match(str);

Console.WriteLine(result.Value);

Output:

Pull PEG Kit, 24", Ref:P - PEG - 24, Exp: 2019 / 03

If the surrounding double quotes are part of the source string:

var regex = new Regex("(.*)");
var str = "\"Pull PEG Kit, 24\", Ref:P - PEG - 24, Exp: 2019 / 03\"";
var result = regex.Match(str);

Console.WriteLine(result.Value);
Console.WriteLine(result.Value.Trim('\"'));

Just trim them out.

Marco
  • 22,856
  • 9
  • 75
  • 124