Remove everything between two characters until they are inside some other characters

Basically, my goal is to delete everything inside (), with the exception of lines inside "".

I followed the code here: Remove text between delimiters in a string (using regex?)

And it works great; but I have an additional requirement not to delete () s if they are in "". This is something that can be done with regex. I feel that I am dangerously close to the need for a different approach, like a true parser.

This is what I used ....

string RemoveBetween(string s, char begin, char end)
{
    Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
    return regex.Replace(s, string.Empty);
}
+5
source share
3 answers
+3

C, java:

input.replaceAll("(?<=\\().*?(?=[\"()])(\"([^\"]*)\")?.*(?=\\))", "$2");

:

"foo (bar \"hello world\" foo) bar" --> "foo (hello world) bar"
"foo (bar foo) bar" --> "foo () bar"

, - , $1 $2

, , C.

+3

", ", - , .: -)

, , Automata-based programming.

20- , 10 .

+2
source

All Articles