简体   繁体   English

C#正则表达式删除单引号之间的单引号

[英]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 嗨,大家好,我想使用C#正则表达式删除单引号之间的所有单引号

'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 ' 请注意,该字符串现在以单词开头,后跟 ,然后是第一个单引号'

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("'","") + "'";

Based on your comments: 根据您的评论:

  1. Remove all single quotes from the string. 从字符串中删除所有单引号。
  2. Add single quotes to beginning and end of string. 在字符串的开头和结尾添加单引号。
string yoursentence = "'this is an 'example' text'";
yoursentence = "'" + yoursentence.Replace("'","")  + "'";
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 . 在此处测试此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM