Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing "\(|.*?)|)" - Too many )'s

Tags:

regex

parsing "\(|.*?)|)" - Too many )'s.

I am receving this error when writing this...

 private static Regex resourceTextsREGEX = new Regex(@"\(|.*?)|)", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase); 

I want a regular expression for these two things 1- {Text} 2- |Text| I want to be able to get those matches and replace them with something ...can someone help please?

like image 591
user510336 Avatar asked Dec 28 '22 18:12

user510336


2 Answers

You haven't said which flavor of regex it is, but the odds are pretty high that ( and ) are grouping operators. Your expression has mis-matched parentheses:

    @"\(|.*?)|)"

You might have meant

    @"(|.*?\)|)"
      ^    ^-- escape
      +-- no escape

...if you're trying to create a group that may include an actual ) in it, or

    @"\(|.*?\)|\)"
      ^     ^  ^
      +-----+--+--- escapes everywhere

...if you're not trying to create a group at all, but just trying to match parens.

like image 80
T.J. Crowder Avatar answered Jan 13 '23 16:01

T.J. Crowder


According to your example what you want to match you might have to want something like this

[{|](.*?)[|}]

See it here on Regexr

So you want to match
1. {Text}
2. |Text|

My regex is matching at first

[{|] either a { or a |

Then comes a capturing group that gets your text using a lazy match (.*?)

And at last the closing character is matched [|}] meaning either } or |

like image 28
stema Avatar answered Jan 13 '23 14:01

stema