Suppose I had the following string I wanted to run a Regular expression on:
This is a test string with \"quotation-marks\" within it.
The \"problem\" I am ha
I will split the string into an array of string, using quotation-mark " as delimiter. Then all the strings with an odd number index will be the string within a pair of quotation mark, use your regex on aSplittedString[oddIndex] only, then join the whole array with ".
Busted my brain to work this one out, turns out that specifying non-word boundaries \B
does the trick:
\B("[^"]*)-([^"]*")\B
$1 $2
http://regex101.com/r/dS0bH8
You are having the same problem as someone who is trying to match HTML or opening and closing parentheses, regex can only match regular languages and knowing which "
is a closing and an opening one is out of its reach for anything but the trivial cases.
EDIT: As shown in Vasili Syrakis's answer, sometimes it can be done but regex is a fragile solution for this type of problem.
With that said, you can convert your problem in the trivial case. Since you are using .NET, you can simply match every quoted string and use the overload that takes a match evaluator.
Regex.Replace(text, "\".*?\"", m => m.Value.Replace("-", " "))
Test:
var text = @"This is a test string with ""quotation-marks"" within it.
The ""problem"" I am having, per-se, is ""knowing"" which ""quotation-marks""
go with which words.";
Console.Write(Regex.Replace(text, "\".*?\"", m => m.Value.Replace("-", " ")));
//This is a test string with "quotation marks" within it.
//The "problem" I am having, per-se, is "knowing" which "quotation marks"
//go with which words.
What you need to do is explicitly only match strings inside quotes that have -
.
Use this:
(\"[^"]*.*?)-(.*?\")
Working example: http://regex101.com/r/jK5eL9
The only catch here is that it will only work for single instances of word-word
in quotes. If you had, say, "word-word, and word-word"
it will fail.
Instead of a regex, a regular method to do this might be more maintainable in the long run:
public static String replaceDashInQuotes(this string source, String newValue)
{
StringBuilder sb = new StringBuilder();
bool inquote = false;
for (int i = 0; i < source.Length; i++)
{
if (source[i] == '\"')
inquote = !inquote;
if (source[i] == '-' && inquote)
sb.Append(newValue);
else
sb.Append(source[i]);
}
return sb.ToString();
}
Then to use it:
var s = @"This is a test string with ""quotation-marks"" within it.
The ""problem"" I am having, per-se, is ""knowing"" which ""quotation-marks""
go with which words.";
MessageBox.Show(s.replaceDashInQuotes(" "));