christiangobo
christiangobo

Reputation: 530

C# Regex to remove single quotes between single quotes

Hi guys i want to remove all single quotes between single quotes using C# regex, example

'this is an 'example' text'

Note that word example is between single quotes. I need that my string looks like:

'this is an example text'

Thanks!

EDIT:

It have some changes! now the string look like:

begin:'this is an 'example' text'

Note that the string now start with a word followed by : and then the first single quote '

Upvotes: 1

Views: 5512

Answers (4)

Ωmega
Ωmega

Reputation: 43673

using System;
using System.Text.RegularExpressions;

public class Test
{
  public static void Main()
  {
    string str = "begin:'this is an 'example' text'"; 
    str = Regex.Replace(str, "(?<=')(.*?)'(?=.*')", "$1");  
    Console.WriteLine(str);
  }
}

Test this code here.

Upvotes: 2

Hassan Boutougha
Hassan Boutougha

Reputation: 3919

string yoursentence = "'this is an 'example' text'";
yoursentence = "'" + yoursentence.Replace("'","")  + "'";

Upvotes: 2

Alex Kalicki
Alex Kalicki

Reputation: 1533

You don't need to use regex (and it's not very well suited to this situation anyway). Try this instead:

string oldString = "'this is an 'example' text'";
string newString = "'" + oldString.Replace("'","") + "'";

Upvotes: 3

mbeckish
mbeckish

Reputation: 10579

Based on your comments:

  1. Remove all single quotes from the string.
  2. Add single quotes to beginning and end of string.

Upvotes: 2

Related Questions