0

I am trying to create a regular expression to match every ( and ) in a string, but exclude \( and \). This is so that I can replace every parentheses but keep the ones with the slash in front of them.

Example String: (,)(5)(5)( )(\()(9)(1)(87x)(100 )(ASP\)( )(5)

Edit: Desired Result after replace: ,55 \(9187x100 ASP\) 5 Then i can remove the \ and get my desired string ,55 (9187x100 ASP) 5

Edit: I am using VB.NET

  • 1
    What language are you using? Some languages will allow you to use a [lookbehind](http://stackoverflow.com/a/546265/1721527) or [lookahead](http://stackoverflow.com/q/9440084/1721527). – Joshua Dwire Mar 06 '13 at 22:53

3 Answers3

2

This will match parenthesis without slashes in front of them (and the character before them):

[^\\]\(
alestanis
  • 21,519
  • 4
  • 48
  • 67
1

Well, you have to take context into account. So first you want to not have a \ and then you want a ( or a ).

[^\\]\(
[^\\]\)

But you can put both paranthesis in a character class

[^\\][\)\(]
wsdookadr
  • 2,584
  • 1
  • 21
  • 44
0

Using a look-behind you can match only "(" or ")" that are not immediately proceeded by a "\":

/(?<!\\)[\(\)]/
Daedalus
  • 1,667
  • 10
  • 12