RegEx Replacement

Posted on 2014-04-09
Last Modified: 2014-04-09
Need to remove a bunch of single ' and double " from some text strings.  I'm using C# 2013 to loop the DB and the only real catch stopping me from using a simple replace is the fact I need the single ' and double " to remain if the the value on the left is a number.  I'm assuming this can be done with a negative lookback, but I'm not have much luck.  Here is a sample string.

Question by:WH_Tech
LVL 75

Accepted Solution

käµfm³d   👽 earned 375 total points
ID: 39989132
Yes, a negative lookback should be what you need:

string result = Regex.Replace(original, "(?<!\\d)['\"]", string.Empty);

Open in new window

Also, if you'd like an explanation of lookaround, you might take a look at the article I wrote on the subject.

Author Comment

ID: 39989251
I found one exception.  Sometimes there are 2 single quotes instead of a double quote.  How can I replace the sequential single quotes with one double quote?
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 125 total points
ID: 39989262
You don't need a regex for that. Just replace '' with ".


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now