Reputation: 44288
if I have the following
the quick brown (fox jumped) over the (lazy) dog
I want a match that has
the quick brown over the dog
or a list of multiple matches
the quick brown
over the
dog
and then I can stitch those strings together...
How to create a match that gets that?
I could do
(\(.*?\))
to find all the things that ARE in the brackets and then just use those strings to remove them from the original string, but I'd kind of like to achieve it all within a single regex ( that way the regex can be loaded as a setting, and there's no need to post process the string )
Upvotes: 2
Views: 99
Reputation: 48686
Try something like this:
(?<!\([^\(\)]*).(?![^\(\)]*\))
To get a full combined string, try this:
string strRegex = @"(?<!\([^\(\)]*).(?![^\(\)]*\))";
RegexOptions myRegexOptions = RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"the quick brown (fox jumped) over the (lazy) dog";
var fullString = string.Concat((object[])
myRegex.Matches(strTargetString).OfType<Match>().ToArray());
Console.WriteLine(fullString); // Produces "the quick brown over the dog"
If you need the multiple spaces converted into single spaces, try this:
string betterString =
System.Text.RegularExpressions.Regex.Replace(fullString,@"\s+"," ");
Console.WriteLine(betterString); // Produces "the quick brown over the dog"
Upvotes: 3
Reputation: 100312
I didn't test extensively, but this seems to work. (Multiple matches)
var str = "the quick brown (fox jumped) over the (lazy) dog";
var result = Regex.Matches(str,
@"("
+ @"\s*\w+\s*"
+ @"(?<!\([^\(\)]*)(?![^\(\)]*\))"
+ ")+"
, RegexOptions.IgnoreCase);
// results (Matches)
// ^the quick brown $
// ^ over the $
// ^ dog$
Upvotes: 1
Reputation: 393
There isn't a single regex to do it for the generic case. Regexs are for extracting a match, not the other way round.
If your goal is to remove the text inside some character such as ([< etc, then you could do a simple string replace, iterating over the answers:
var myRegex = "(\(.*?\))";
var myString = "the quick brown (fox jumped) over the (lazy) dog";
while (var match = regex.Match(myString, myRegex)) {
myString.replace(match, "");
}
Or something like that! That way you can still select what the brackets are. This could be wrapped up in a method to make it cleaner for your code to use.
(That code should be taken as pseudo code, it's just hit Friday drinks time!)
Upvotes: 0