Steve23
Steve23

Reputation: 113

java regex parenthesis

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)

Upvotes: 2

Views: 76

Answers (1)

shrug
shrug

Reputation: 47

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

Upvotes: 1

Related Questions