正则表达式.替换标点符号

本文关键字:标点符号 替换 正则表达式 | 更新日期: 2023-09-27 18:07:59

我有以下输入字符串

some text ) more text
some text , more text
some text ! more text
some text ; more text
some text ? more text
some text . more text
some text)more text
some text,more text
some text!more text
some text;more text
some text?more text
some text.more text
some text )more text
some text ,more text
some text !more text
some text ;more text
some text ?more text
some text .more text

我正在使用正则表达式。替换希望得到

的方法
some text) more text
some text, more text
some text! more text
some text; more text
some text? more text
some text. more text
some text) more text
some text, more text
some text! more text
some text; more text
some text? more text
some text. more text
some text) more text
some text, more text
some text! more text
some text; more text
some text? more text
some text. more text

但是我的字符串保持不变。

这是我的班级:

public class PunctionationSignsSpaceing : ILanguageRuleFormater
    {
        private string _pattern;
        public PunctionationSignsSpaceing()
        {
            _pattern ="( *[),!;?.] *)";
        }
        public string FormatString(string str)
        {
            str = Regex.Replace(
                str,_pattern,"$1",
                RegexOptions.Multiline|RegexOptions.Compiled
            );
            return str;
        }
    }

我在这里做错了什么吗?(我对正则表达式有点陌生。)由于

正则表达式.替换标点符号

正则表达式无效。你正在用自身替换整个匹配,这就是为什么你在结果字符串中看不到任何变化。

试试这个:

public class PunctionationSignsSpaceing
{
    private string _pattern;
    public PunctionationSignsSpaceing()
    {
        _pattern = " *([),!;?.]) *";
    }
    public string FormatString(string str)
    {
        str = Regex.Replace(
            str, _pattern, "$1 ",
            RegexOptions.Multiline | RegexOptions.Compiled
        );
        return str;
    }
}

您还应该考虑将_pattern初始化从对象构造函数移到字段本身:

public class PunctionationSignsSpaceing
{
    private string _pattern = " *([),!;?.]) *";
    public string FormatString(string str)
    {
        str = Regex.Replace(
            str, _pattern, "$1 ",
            RegexOptions.Multiline | RegexOptions.Compiled
        );
        return str;
    }
}

这是一个不使用正则表达式的解决方案

public static void Main (string[] args)
{
    char[] punctiationMarks = new char[]{'.', ',', '?', '!', ';', ')'};
    string inputString = "foo; bar;foo ,  bar , foo'nbar, foo ) bar foo  )bar";
    StringBuilder outputString = new StringBuilder ();
    int indexOfPunctuationMark = -1;
    inputString
        .Split (punctiationMarks, StringSplitOptions.None)
        .ToList ()
            .ForEach (part => {
                indexOfPunctuationMark += part.Length + 1;
                outputString.Append (part.Trim ());
                if (indexOfPunctuationMark < inputString.Length)
                    outputString.Append (inputString [indexOfPunctuationMark]).Append (" ");
            }
    );
    Console.WriteLine (outputString);
}