开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜