-1

Can someone please help me in PHP to build a regex and parse the below string to achieve individual modules.

Examples of some string formats: ( ) => group

  1. str1 = ( A && B ) || ( C || D ) && !( E || F )

    O/P array = ( group1, || , group2, && , group3)

  2. str2 = !( A || B ) && ( C || D ) && ( E && F )

    O/P array = ( group1, && , group2, && , group3)

  3. str3 = !( A || B )

    O/P array = ( group1)

  4. str4 = ( A && B )

    O/P array = ( group1)

  5. str5 = ( A || B )

    O/P array = ( group1)

  6. str5 = ( A && B )

    O/P array = ( group1)

In short, these groups can be any order or there is no limit for the data within each group.

Group 1 : ( A && B && C &&......n )

Group 2 : ( A || B || C ||......n )

Group 3: !( A || B || C ||......n )

Thanks in advance. If needed then I can post what I have right now?

Answer which worked as of now: '/(!?\(.*?\))|\|\||&&/'

But it is giving me an error for below examples where there is just one group which is w/o parentheses:

  1. str1 = A && B

    O/P array = group1 (no parentheses) where group 1 = A && B

  2. str2 = A || B || C

    O/P array = group1 (no parentheses) where group 1 = A || B || C

user1704440
  • 79
  • 1
  • 2
  • 8
  • 1
    Yes, you should always post your relevant attempts, otherwise this question might get closed quickly. – kapa Mar 06 '13 at 21:54
  • 1
    Generally, regular expressions can only describe [regular languages](http://en.wikipedia.org/wiki/Regular_language). Your language is not regular but [context-free](http://en.wikipedia.org/wiki/Context-free_language).So you’ll probably need to write your own parser. – Gumbo Mar 06 '13 at 22:06
  • Nesting craziness with regex (it validates the input, so it is longer than normal), just to demonstrate it is possible, but not recommended: http://stackoverflow.com/questions/15233953/php-split-string-on-comma-but-not-when-between-braces-or-quotes/15235628#15235628 – nhahtdh Mar 06 '13 at 22:51
  • My solution as of now is: '/^(\( \w+( \|\| \w+)* \)|\(\( \w+( && \w+)* \)\)|[!]\( \w+( \|\| \w+)* \))? /( (&& |\|\|) (\( \w+( \|\| \w+)* \)|\(\( \w+( && \w+)* \)\)|[!]\( \w+( \|\| \w+)* \)))*$/' – user1704440 Mar 07 '13 at 01:27
  • @user1704440 Add what you've tried to your question – Jon Cram Mar 07 '13 at 12:41

1 Answers1

0

I'm not clear why you expect the ! to be dropped in your example 3. I will assume that you actually meant the O/P array for that one to be a group3. Can I suggest that in future, you show actual output for your example inputs. Anyway, based on what you have put, plus my assumption, it seems that this may do what you want:

preg_match_all('/(!?\\(.*?\\))|\\|\\||&&/', $str, $matches);

where $str is your input format.

MikeM
  • 13,156
  • 2
  • 34
  • 47
Captain Payalytic
  • 1,061
  • 8
  • 9
  • Hi, thanks for your answer it indeed work. I am sorry but I forgot to mention one more condition where my code is breaking currently. I would appreciate if you can add that also. I have attached more conditions in my question. – user1704440 Mar 13 '13 at 20:23
  • Can't you just add parentheses in the case where there are none? – Captain Payalytic Mar 19 '13 at 21:10
  • I did that.. This will work if there are no parentheses at all like str1 = A || B or str2 = A || B && C... But not for str1 = ( A || (B) && C ) Because regex is always looking for some parenthesis around every group – user1704440 Mar 24 '13 at 06:05
  • But this case is not part of any of your documented conditions. Please can you sort out precisely what it is you want to do and then re-ask the question as a complete whole. I would rather solve it once than waste my time solving one thing only to have you continually move the goalposts. – Captain Payalytic Mar 24 '13 at 17:13
  • I missed that condition completely from my side. My apology. That was the overall condition I was looking for. I realized this edge case when I actually started working. I had listed all the conditions before posting this question but my bad, i missed this last one. I should say that your code works smoothly with all other condition I was looking for. Thanks a lot for that. – user1704440 Mar 25 '13 at 22:53