2013-04-07 88 views
2

Regex.Replace标点符号

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 

我使用Regex.Replace方法希望能得到

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; 
     } 
    } 

难道我做错了什么吗? (我对正则表达式有点新) 谢谢

+0

谢谢为例子,但我仍然不明白你的问题。请你也可以解释你想用散文做什么? – 2013-04-07 15:07:42

回答

3

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

尝试一个:

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; 
    } 
} 
+0

感谢你了。 – trebor 2013-04-07 14:16:54

+0

您也可以使用'\ s'而不是空格。它看起来更清楚发生了什么。 – Sukima 2013-04-07 14:18:46

+0

@Sukima从我所知道的不是空白字符(不只是空格字符)有更多的字符显示空格,我只是想空间字符 – trebor 2013-04-07 14:34:27

0

这里是一个解决方案,无需使用正则表达式

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); 
}