Convert Regular expression to a simple one
I have this expression (a and b) or not (c and d)
I want to convert it to a simple one a and b or not c and or not d
Is that possible in Regex
thanks
I think you're mixing the concepts of mathematical expressions and regular expressions. These two things have no relation to each other. Regular expressions are a tool for searching and replacing pieces of strings.
It looks like you're trying to apply De Morgan's laws to a boolean expression, changing "not (C and D)" to "not C or not D". That is not text manipulation per se, it is Boolean algebra and is better solved by lexing/parsing techniques.
This is too large a topic to summarize in one Stack Overflow answer, but as an overview I'd recommend creating an abstract syntax tree (AST). An AST for your first expression would look like this:
or
/ \
and not
/ \ |
a b and
/ \
c d
Then you can manipulate the nodes of that tree by applying the rules of Boolean algebra. For instance, De Morgan's law "not (C and D) = not C or not D" is the same as the following sub-tree transformation:
not or
| / \
and --> not not
/ \ | |
c d c d
精彩评论