Reputation: 3013
I have the following input string
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
I'm using the Regex.Replace method hoping to get
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
But my string remains unchanged.
This is my class:
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;
}
}
Am I doing something wrong here? (I'm a bit new to regex.) Thanks
Upvotes: 2
Views: 136
Reputation: 10427
Here is a solution without using regex
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);
}
Upvotes: 0
Reputation: 125630
Your regular expression is invalid. You're replacing whole match with itself, and that's why you don't see any change in your result string.
Try that one:
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;
}
}
You should also consider moving _pattern
initialization from object constructor into field itself:
public class PunctionationSignsSpaceing
{
private string _pattern = " *([),!;?.]) *";
public string FormatString(string str)
{
str = Regex.Replace(
str, _pattern, "$1 ",
RegexOptions.Multiline | RegexOptions.Compiled
);
return str;
}
}
Upvotes: 3