2

I tried to split a propositional formula to severals sub-formulas in java. Can I use regex or should I write a parser? My grammar is only composed of literal symbols (A, B, C,...) and connectors (|, &, =>, <=>, not)

Example 1:

"((A | B) & (B | C))" :

array[0]= (A | B) 
array[1]=    &
array[2]= (B | C) 

Example 2:

"((A => B) => (B | C)) & (B => A)" :

array[0]= (A => B) 
array[1]=    =>
array[2]= (B | C) 
array[3]=    &
array[4]= (B => A)
3
  • 1
    Probably you'll need to write a parser, but I can't be positive because it's unclear what rules you are applying to decide what the results should be in your two examples. Writing out the rules in prose would be a good first step. Commented Nov 27, 2016 at 18:46
  • @JohnBollinger In fact, I try to split my formula to sub-formula to build a semantic tableau Commented Nov 27, 2016 at 19:40
  • @JohnBollinger Otherwise, it can be nice if I can get the 'middle' connector. For example "&" in the two formula. Commented Nov 27, 2016 at 19:51

1 Answer 1

1

This regex works on the examples given:

(?:\([^()]+\))++|(?:&|<?=>|\|)++

https://regex101.com/r/NSQ9aZ/3

matches a string starting with a ( and ending with a ). Also matches &,|,=>,<=>, not

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.