C
C#•11mo ago
sashok

Regex - Match a string outside of the parentheses

How do I match any e which's not inside of the parentheses? I have created a pattern to do it, but it doesn't seem to work properly. It should store (s and remove )s, so that an e that is outside of the parentheses is matched e.g. ()()()e, but e inside of the parentheses isn't e.g. (e). https://regex101.com/r/3m6ZUc/1
(?<=(?:[^()]|(?<a>\()|(?<-a>\)))+(?(a)(?!)))e
(?<=(?:[^()]|(?<a>\()|(?<-a>\)))+(?(a)(?!)))e
1 Reply
sashok
sashokOP•11mo ago
Even though quite a similar pattern finds them properly.
(?:[^()]|(?<a>\()|(?<-a>\)))+(?(a)(?!))
(?:[^()]|(?<a>\()|(?<-a>\)))+(?(a)(?!))
https://regex101.com/r/BE4OLO/1 Oh, was writing the answer and discord suddenly crashed. Yes, parentheses can be nested. E.g.
e+((((e)+e)+e))+e
e+((((e))))()(())+e
e+((((e)+e)+e))+e
e+((((e))))()(())+e
In this case just es ouside of the parentheses should be matched so (e) is inside of them, ()e - ouside And the format can be wrong too.
)e(
)e(
in this case there're not enough parantheses, no e should be matched
)(()e
)(()e
In this case I don't really care whether e is matched or not. E is probably outside of the parantheses, but the format is wrong and I'm gonna throw an error for this input. So it doesn't matter whether e is matched here Yes, the purpose of the pattern is just to match the e outside of the brackets. It shouldn't include looking for errors so how would your recommend to implement it? I see, I guess I got a bit too excited about using regex in all the cases I'll use this variant then Thank you 🙂
Want results from more Discord servers?
Add your server