Click here to Skip to main content
15,867,934 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This is a weird question.

I need to resolve things like (foo[a-z]bar|baz[a-z]bar) to (foo|bar)[a-z]bar

I'm hoping for an easier way to do it, with linq or without. I have an abstract syntax tree with things like RegexConcatExpression and RegexOrExpression so I can query the expression in a structured manner as a tree.

The problem is the way to do it I've come up with is extremely complicated.

What I have tried:

The actual code is long, and didn't work at the time of this ask so instead I'll describe what I was trying to do.

I was comparing each expression in the | list. If they were both RegexConcatExpression objects I'd go through the expressions in each.
I was counting backward (right to left) from the end of each expression, calling Equals (which I have on each AST class) until I found they didn't match. Then I would replace the the expression with a concat expression including the union of all the different leading bits followed by the trailing bit that's the same for each.

One of the problems with this, besides being inefficient is it doesn't handle all cases very well. Like in the case where there are multiple nested similarities.
Posted
Comments
Chris Copeland 7-Jan-22 6:26am    
I would think any effort to try and combine regular expressions would require an immensely complicated piece of code. Once you start considering lookaheads, back references and relative backreferences, things would get extremely tricky!
honey the codewitch 7-Jan-22 8:25am    
I should have clarified. This is DFA regex. No backtracking constructs. I can already convert to a regex string from a state machine. this is only about simplifying the result.
BillWoodruff 8-Jan-22 0:37am    
Writing a function that takes string data and a list of regex's as inputs, and then executes the regex's in sequence is not acceptable ?
honey the codewitch 8-Jan-22 0:38am    
Yeah, I'm not trying to execute the regular expressions. I'm trying to simplify and compact them programmatically.
BillWoodruff 8-Jan-22 0:53am    
Got it. Wonder how many DFA gurus are using the internet from their Himalayan caves OMG :)

Happy New Year !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900