1

I want to write Regex expression using C# to find the first ) character that isn't wrapped around single or double-quotes.

I have the following string ("some () test 1", 'some text)', 1235,"",null ) testing something The only ) that I want to find is the one after the null.

I am unsure how to express "not between" using regex. Here is what I tried

[^(("|').*\).*("|'))]\)

But that is matching more that I am expecting.

How can I correctly find the close parentheses that aren't wrapped around the quotes?

Using C# Regex.Match(str, pattern) should return the first occurrence if my expression was valid.

Updated Here are some texts that I want to be able to evaluate

  1. ("some () test 1", 'some text)', 1235,"",null ) testing something
  2. ("some test 1", 'some text', null, "")
  3. ()
  4. (1)
  5. (1,2)
  6. ("text")
  7. ('text ) ')
  8. ("te)t")
  9. ("te )st") blah blah
Junior
  • 11,602
  • 27
  • 106
  • 212
  • So, the regex should work in C#, right? If you want to parse some langugae, you'd better use the dedicated parser. Regex is best at parsing plain text. Here, it is not a matter of finding a char *not between*, there are string literals inside the parentheses, and it is not that easy with a regex (although possible to some extent) – Wiktor Stribiżew Oct 19 '19 at 21:30
  • Due to the way the question is asked I doubt a good answer can be given. You must add more details about what kind of input you want to handle. In a generic case the answer is "do not use a single regex approach" here. – Wiktor Stribiżew Oct 19 '19 at 21:51
  • @WiktorStribiżew I updated my question with some possible text – Junior Oct 19 '19 at 21:58
  • @WiktorStribiżew another way to look at the problem would be to parse out the parameters of the function "if any" If I have an array of the parameters, I will look for the `)` immediately follow the last parameter found. – Junior Oct 19 '19 at 22:11
  • @Junior Maybe just use `s.LastIndexOf(")")`? – Wiktor Stribiżew Oct 19 '19 at 22:15
  • LastIndexOf() could return `)` that is after the `)` that closes the function. I also thought about the string.Split(",", "text") and then look for IndexOf after the last param. But if one of the string parameters contains a comma then I would have a problem too here is an example of a string that would break it in both cases `("this is, a test","", 123, null) some other text between (i.e., text)` – Junior Oct 19 '19 at 22:17
  • That is why I mentioned the *dedicated language parser*. You will have to write one. – Wiktor Stribiżew Oct 19 '19 at 22:21
  • I am trying to write one. But need to find a way to identify when should the search stop as it I finally idetified where the last param end and the function ends. – Junior Oct 19 '19 at 22:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201150/discussion-between-junior-and-wiktor-stribizew). – Junior Oct 19 '19 at 22:35
  • 1
    You can match what you don't want but [capture what you need](https://regex101.com/r/HznCnf/1). Also you can [check for even amount of quotes ahead](https://regex101.com/r/HznCnf/2) by use of a lookahead to assume *outside* [like this answer](https://stackoverflow.com/a/26609791/5527985). – bobble bubble Oct 19 '19 at 22:38

1 Answers1

0

Itt seems like you're trying to find the last parameter and the end of the function, but I am not privileged to comment, so I am gonna write it here.

int FindIndexOfBackwards(string s, char tobefound, int index)
{
bool avoided = true;
int counter = index;
do
{
if( s[counter] == '"' && avoided == true)
{
     avoided = false;
}
else if (s[counter] == '"' && avoided == false)
{
 avoided = true;
}
if(s[counter] == tobefound && avoided == true ) return counter;
counter--;
}while( counter > -1);
return -1;
}

You can perhaps use LastIndexOf(")") as the third parameter of the function. For example:

string parameters = "(\"some () test 1\", 'some text)', 1235,\"\",\"n,ull\" ) ";
int LastIndexOfComma =FindIndexOfBackwards(parameters, ',', parameters.LastIndexOf(")") );  // it avoids " and find comma that is out of it
string lastparameter = parameters.Substring(FindIndexOfBackwards(parameters, ',', parameters.LastIndexOf(")") ), parameters.LastIndexOf(")") - 1);

You can divide all the parameters using this method recursively. Hope that helps.

B83C
  • 1
  • 2