regex


Regex look for all quotes not preceded by a comma


I have a CSV file that is not well formatted and I need to look for extra quotes.
This is what it looks like:
"1","title","desc desc dejdg sdjgh djhg"
"2","title2","desc jhgdj "jhsgfjhsgd" jhgd"
^^^^^^^^^^^^
I need to look for any " that doesn't have a , before:
(\")(?!\,)
This would mean look for any " that doesn't have a , right after, but I don't know how to look for a , before the ".
The regex you are asking for would be
(?<!,)"
Alternatively, if you're not averse to matching more than you need you can use
(^|[^,])"
which would match the preceding non-comma as well as the double-quote, but support for it should be more widespread (excluding findstr).
However, for parsing CSV files you should use a proper parser as any regex-based solution (at least those I have seen so far for this task) is
error-prone
unreadable
slow
What about
[^,](\")
?
Notice that this doesn't detect quotes at the beginning of the line (which, technically, are quotes not preceded by commas), but for your usage this is fine, since quotes at the beginning of the line aren't an error in a CSV file.
In .NET, you might use the TextFieldParser Class. Add the Microsoft.VisualBasic reference to your project and try this:
using System;
using System.Linq;
using Microsoft.VisualBasic.FileIO;
class Program
{
static void Main(string[] args)
{
using (var tfp = new TextFieldParser("input.txt"))
{
tfp.Delimiters = new string[] { "," };
tfp.HasFieldsEnclosedInQuotes = false;
while (!tfp.EndOfData)
{
var fields = tfp.ReadFields();
fields.ToList().ForEach(field =>
{
Console.WriteLine(field);
});
}
}
}
}

Related Links

how do i extract only 5-digit strings from cells in excel?
Regex to gather text within multiple pipes
Extract specific columns from line and match against given range
Redirecting from /index.php to / not working
Change and move timestamp in Notepad++ [closed]
Regex extract between trible double quotes and newlines
Small tweak to get this regex to get it to do what I want
atom - How to replace in Atom without replacing some part of the search?
Match words separated by semicolon and ending with 'ase anydigit'
Vim script highlight regex matches on cursor line
how to change pattern in file's line
Remove <s> and </s> from all lines in file
rsync --exclude-from 'list' file not working
Replacing a character between numbers in UNIX
Regex for Google Analytics Goals
Regex evaluate string and inner strings in quotes for balance and length

Categories

HOME
gwt
crf
tags
robotframework
xquery
sweetalert
whitespace
freebsd
socrata
zipfile
eps
doorkeeper
google-weather-api
cytoscape.js
editor
scale
deserialization
w2ui
round-robin
libpcap
revitpythonshell
windows-applications
nxbre
rgdal
aspdotnetstorefront
c-strings
zos
google-oauth2
parallax
google-geocoder
valueinjecter
plm
panel-data
lync
sqlite.swift
webcenter
rhel7
paypal-rest-sdk
uitabbarcontroller
procobol
jpype
mamp-pro
syncfusion
slt
azure-cdn
website-hosting
tpl-dataflow
htc
email-notifications
vs-2015-preview
plotrix
mailcore2
xceed
flattr
tealeaf
xpdf
webclient-download
uploadify
main-activity
vimeo-ios
linqtocsv
sample-data
hjson
esky
base-conversion
gem5
ehcache-bigmemory
byte-order-mark
declaration
bitrock
liferay-hook
opensc
syndication-feed
globus-toolkit
ash
pkcs#10
modalpopupextender
inputview
deap
bochs
buildout
chomsky-hierarchy
asp.net-authentication
sizzle
blackberry-cascades
arden-syntax
server.mappath
websolr
livevalidation
ladon
boost-gil
springboard
soapheader
openfeint
nscollectionviewitem
cluetip
program-transformation
automapping
n900
chickenfoot
sloc
sqlitemanager
help-authoring

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App