Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The function by Mathematica to expand a formulation with ANDS, ORS, NOT, XOR, and others, is LogicalExpand, let see an example:

LogicalExpand[ p && (q || x)]
(p && q) || (p && x)

I want to make the same thing but with strings, and these strings can be regular expression (it's contains parenthesis in some cases). I mean the form of the strings. Let set a test case.

expr = "(?i)god && (evil || salvation(.) )"
MyLogicalExpand[ expr ]

And the result should be:

"(?i)god && evil || (?i)god && salvation(.)"

Notice that result don't group the atomic expression with parenthesis and the regular expression are the same.

I don't know which way I should take. It's a parser problem, and the regular expression make it more complex, some space after o before inside atomic expressions could be appear.

How do you program MyLogicalExpand method ?

share|improve this question
Why mix logical operators inside regular expressions? What does "(?i)god && (evil || salvation(.) )" mean? A string cannot match both "god" and ("evil" or "salvation")? – asterix314 Oct 14 '13 at 4:03
That's not have any sense, just is a string expression compound by others string than could be regular expression. The matter is that its class of string expression could have parenthesis, not for the task to logical expand. – jonaprieto Oct 14 '13 at 4:29
up vote 3 down vote accepted
 MyLogicalExpand[expr_] := 
   With[{patt = "(" ~~ x : (Except[Characters["()"]] ..) ~~ ")" /; (
                Implies[#, ! #2] & @@ (
                MemberQ[StringPosition[x, LetterCharacter][[;; , 1]], #] & /@ {2, 1})) 
                cas = StringCases[expr, patt],
                pos = StringPosition[expr, patt], n},
               n = Length@cas;

                   StringReplace[#, Table[("Mark" <> ToString[i]) -> cas[[i]], {i, n}]] &,
                   StringReplacePart[#, Table["Mark" <> ToString[i], {i, n}], pos] &

The following method has couple od points which need to be improved, e.g I assumed that the expresion within ( ) is RegEx when 3rd character is LetterCharacter implies 2nd character is LetterCharacter which is just my observation. Also, markers are not so uniqe with this form.

The method is just to replace found RegExes with markers, and use LogicalExpand on converted expresion, then replace back markers with RegExes.

expr = "(?i)god &&(evil || salvation(.) )"

"(evil && (?i)god) || ((?i)god && salvation(.))"

I don't know why you want to skip () but if you need you can add a line to Composition:

            StringReplace[#, Table[("Mark" <> ToString[i]) -> cas[[i]], {i, n}]] &,
(*new line*)StringReplace[#, "(" | ")" -> ""] &,
            StringReplacePart[#, Table["Mark" <> ToString[i], {i, n}], pos] &

"evil && (?i)god || (?i)god && salvation(.)"
share|improve this answer
@d555 I don't think that it is possible to distinguish your expression in ( ) from every possible RegularExpression object. I can improve my code if you, for example, agree to use () only for regex and other will be [] or {}. – Kuba Oct 14 '13 at 8:38
Yes, you're right. I think that It's better if I handled it with RegularExpression's Head inside the string. – jonaprieto Oct 14 '13 at 17:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.