[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 802
  • Last Modified:

double quotation replace into single quotation

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
saloj
Asked:
saloj
2 Solutions
 
crysallusCommented:
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
 
käµfm³d 👽Commented:
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
 
Sudhakar PulivarthiCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
salojAuthor Commented:
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
 
crysallusCommented:
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
 
käµfm³d 👽Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now