I need some help replacing only a portion of a string that I match with regex. For example, I have the following text:
This is some sentence
and the continuation of the sentence
I need to find new line characters such that it is preceded by a word and proceeded by a word, so I use the following regex:
Regex rgx = new Regex("\w\n\w");
When I find this condition, I want to replace ONLY the newline with a space. So the output would look like this:
This is some sentence and the continuation of the sentence
Is it possible to do this?
UPDATE 12/11/14:
This question was marked as a duplicate, however, the referenced solution was not exactly what I was looking for. As stated above, it needs to be the scenario where a new line is preceded and proceeded by a character. The referenced solution just catches all '\n' characters and replaces it with an empty string.
Here was the solution for my problem:
string input = "This is some sentence\nand the continuation of the sentence",
pattern = @"(\w)\n(\w)",
replacement = "$1 $2",
output = string.Empty;
output = Regex.Replace(input, pattern, replacement);
The result of this would be:
This is some sentence and the continuation of the sentence
My solution was inspired by this solution.