Solved

double quotation replace into single quotation

Posted on 2011-02-22
6
727 Views
Last Modified: 2012-08-13
Hi EE,
I have following sample paragraph, which has URL with double quotation sign, but I would like to replace double quotation mark into single quotation mark. I want to do it in regular expression.

eg.
<p> sfkjsaf safj afjsfks fsfsf sjf s <a href="http://www.test.com" target="_blank">www.test.com</a>sfs fjsf ksjfsa faf </p>

replace:
<p> sfkjsaf safj afjsfks fsfsf sjf s <a href='http://www.test.com' target='_blank'>www.test.com</a>sfs fjsf ksjfsa faf </p>

pls help
Thanks
0
Comment
Question by:saloj
6 Comments
 
LVL 8

Expert Comment

by:crysallus
Comment Utility
If you just want to replace all double quotes with single quotes, why don't you just call:

str.Replace('"', '\'');

Open in new window

A regex is overly complicated for that.

Or do you only want to replace certain double quotes depending on a certain pattern eg. only for href's (even though your example doesn't show that)?
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
Comment Utility
Try:
string source = "<p> sfkjsaf safj afjsfks fsfsf sjf s <a href=\"http://www.test.com\" target=\"_blank\">www.test.com</a>sfs fjsf ksjfsa faf </p>";
string result = System.Text.RegularExpressions.Regex.Replace(source, "(href=)\"([^\"]+)\"", "$1'$2'");

Open in new window

0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
Comment Utility
Hi Saloj,
If you want to use only Regular Expression for the example which u said saloj there is not much difference to use string.Replace
Using Regex the same output is generated:
Regex.Replace(data, "\"", "'");

Please clarify if any further requirement is there..
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:saloj
Comment Utility
crysallus,sudhakar,

I want to replace double quote to single from the URL only.

Kafumed solution is almost work but it does not help for target="_blank".

eg.
<a href="http://www.test.com" target="_blank">www.test.com</a>

replace:
<a href='http://www.test.com' target='_blank'>www.test.com</a>

help guys!!
Thanks
0
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 100 total points
Comment Utility
If you only want the href and the target attributes affected, you can add the target attribute to kaufmed's regex like this:

(href=|target=)\"([^\"]+)\"

which basically says that if it's href= OR target=, then replace the double quotes.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I see. You specified "URL" in the question, so I thought you were only talking about the "href" portion. I see now that you changed both in your OP. In that case, crysallus' modification will suit your needs.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 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

11 Experts available now in Live!

Get 1:1 Help Now