精确的正则表达式模式匹配一​​个字符串与单引号 [英] Exact Regex Pattern Match of a string with single quotes

查看:164
本文介绍了精确的正则表达式模式匹配一​​个字符串与单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个包含单引号的字符串,例如他和'她'是我的爸爸朋友。我想用Latha取代'她'。那么请你告诉我这是否可行。以下示例适用于没有单引号的完全匹配。但单引号却失败了。请帮忙。





  string  pattern =  @  \ bLatha \ b; 
string input = 他和她是我父亲的朋友们;
string result = Regex.Replace(input,pattern, ,RegexOptions.None);





结果是:他和Latha是我爸爸的朋友



< pre lang =c#> string pattern = @ \bLatha\b;
string input = 他和'她是我爸爸的朋友;
string result = Regex.Replace(input,pattern, '她',RegexOptions.None);





结果是:他和'她'是我的爸爸朋友



期望的结果是:他和Latha是我爸爸的朋友





请告诉我你的建议。



谢谢&此致,

Mathi。

解决方案

试试这个:



  const   string  pattern =   Latha; 
const string input = 他和'她'是我爸爸的朋友;
var regExpr = new 正则表达式( \'。* \',RegexOptions.IgnoreCase);
var result = regExpr.Replace(input,pattern);


这应该工作,单引号与否:

 使用系统; 
使用 System.Text.RegularExpressions;

public class 计划
{
public static void Main()
{
string pattern = @ \ '?she\'?;
string input = 他和'她是我爸爸的朋友;
string result = Regex.Replace(input,pattern, Latha,RegexOptions.None);
Console.WriteLine(result);
}
}


试试这个:

  string  pattern =  '她'; 
string input = 他和'她是我爸爸的朋友;
string result = Regex.Replace(input,pattern, Latha,RegexOptions.None);



我相信你混淆了模式和替换。

检查out:

http://msdn.microsoft .com / zh-cn / library / ewy2t5e0(v = vs.110).aspx [ ^ ]


Hi All,

I have a string which contains single quotes say for example "He and 'she' are my Dad friends". I want to replace 'she' with Latha. So Could you please let me know whether this is possible. The below example works for exact match without single quotes. But with single quote this fails. Please help.


string pattern = @"\bLatha\b"; 
string input = "He and she are my Dad friends"; 
string result = Regex.Replace(input, pattern, "she", RegexOptions.None); 



result is : He and Latha are my Dad friends

string pattern = @"\bLatha\b"; 
string input = "He and 'she' are my Dad friends"; 
string result = Regex.Replace(input, pattern, "'she'", RegexOptions.None); 



result is : He and 'she' are my Dad friends

Expected result is : He and Latha are my Dad friends


Kindly let me know your suggestions.

Thanks & Regards,
Mathi.

解决方案

Try this:

const string pattern = "Latha";
const string input = "He and 'she' are my Dad friends";
var regExpr = new Regex("\'.*\'", RegexOptions.IgnoreCase);
var result = regExpr.Replace(input, pattern);


This should work, single quote or not:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string pattern = @"\'?she\'?";
        string input = "He and 'she' are my Dad friends";
        string result = Regex.Replace(input, pattern, "Latha", RegexOptions.None);
        Console.WriteLine(result);
    }
}


Try this:

string pattern = "'she'";
string input = "He and 'she' are my Dad friends";
string result = Regex.Replace(input, pattern, "Latha", RegexOptions.None);


I believe you mixed up the pattern and replacement.
Check out:
http://msdn.microsoft.com/en-us/library/ewy2t5e0(v=vs.110).aspx[^]


这篇关于精确的正则表达式模式匹配一​​个字符串与单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆