double quotation replace into single quotation

Posted on 2011-02-22
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.

<p> sfkjsaf safj afjsfks fsfsf sjf s <a href="" target="_blank"></a>sfs fjsf ksjfsa faf </p>

<p> sfkjsaf safj afjsfks fsfsf sjf s <a href='' target='_blank'></a>sfs fjsf ksjfsa faf </p>

pls help
Question by:saloj

Expert Comment

ID: 34951548
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)?
LVL 75

Accepted Solution

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

Open in new window

LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 34979075
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..
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 34990971

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".

<a href="" target="_blank"></a>

<a href='' target='_blank'></a>

help guys!!

Assisted Solution

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


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

Expert Comment

by:käµfm³d 👽
ID: 34997452
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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 49
VB.NET 2008 - SQL Timeout 9 34
LINQ Select all records by MAX data syntax help 1 19
need to convert function to c# 5 31
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…
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…

820 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