1

I'm struggling to split the following input string to array:

'((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)'.split(/\(([^)]+)\)/g)

Yields:

["", "(Application = smtp AND "Server Port" != 25", " AND ", "Application = smtp AND "Server Port" != 25", ") OR ", "Application = pop3 AND "Server Port" != 110", " OR ", "Application = imap AND "Server Port" != 143", " AND ", "Application = imap OR "Server Port" != 143", ""]

But what I want the result should look like:

["", "(Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25")", OR ", "Application = pop3 AND "Server Port" != 110", " OR ", "Application = imap AND "Server Port" != 143", " AND ", "Application = imap OR "Server Port" != 143", ""]

notice the 1st index "(Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25")"

Any suggestions to achieve this in RegEx?

EDIT Formatted:

I'm having following input string

(
   (
     (App = smtp AND "Server Port" != 25) 
     OR 
     (App = pop3 AND "Server Port" == 20)
   ) 
   AND 
     (App = smtp AND "Server Port" != 35)
 ) 
OR 
(App = pop3 AND "Server Port" != 110) 
AND 
(
   (App = imap AND "Server Port" != 143) 
   OR 
   (App = pop3 AND "Server Port" == 20)
) 
AND (App = imap OR "Server Port" != 143)

wants to transform into:

[
    [
         [
           'App = smtp AND "Server Port" != 25', 
           'OR', 
           'App = pop3 AND "Server Port" == 20'
         ], 
      'AND', 
      'App = smtp AND "Server Port" != 35'
    ], 
   'OR', 
   'App = pop3 AND "Server Port" != 110', 
   'AND', 
   [      
     [
       'App = imap AND "Server Port" != 143', 
       'OR',
       'App = pop3 AND "Server Port" == 20'
     ]
   ], 
   'AND', 
   'App = imap OR "Server Port" != 143'
]
6
  • Could you share what was regex you've tried? Commented Apr 7, 2017 at 5:15
  • 1
    The expected array you gave is not a valid javascript array. Commented Apr 7, 2017 at 5:18
  • I have formatted the input and output. Commented Apr 7, 2017 at 5:33
  • I have tried .split(/\(([^)]+)\)/g) Commented Apr 7, 2017 at 5:33
  • So your expected output is an nested array, I really don't think regex is appropriate tool to archive this, maybe this stackoverflow.com/questions/40477850/… could be helpful for you. Commented Apr 7, 2017 at 5:39

2 Answers 2

1

As many suggested the better way to do is to create a flattend array. And, deal with each inner array with (split). But, Still for the sake of argument if you want to do it by regex this is one odd / awkward solution to consider:

    var str = '((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)';

    var final = str.replace(/\((?!\()/g,"['")        //replace ( with [' if it's not preceded with (
               .replace(/\(/g,"[")               //replace ( with [
               .replace(/\)/g,"']")              //replace ) with '] 
               .replace(/\sAND\s/g,"','AND','")  //replace AND with ','AND','
               .replace(/\sOR\s/g,"','OR','")    //replace OR with ','OR','
               .replace(/'\[/g,"[")              //replace '[ with [
               .replace(/\]'/g,"]")              //replace ]' with ]
               .replace(/"/g,"\\\"")             //escape double quotes
               .replace(/'/g,"\"");              //replace ' with "
    console.log(JSON.parse("["+final+"]"))

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

1 Comment

Though its hacky (I will enhance it) its more generic than I expected.
0

Try this and see if it helps:

\(([^O]+|[^(]+)\)

The complete code will be something like this:

console.log('((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)'.split(/\(([^O]+|[^(]+)\)/g))

1 Comment

this logic I have already tried but it fails: '((Application = smtp AND "Server Port" != 25) AND ((Application = smtp AND "Server Port" != 25) OR (Application = smtp AND "Server Port" != 25))) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)'.split(/\(([^O]+|[^(]+)\)/g)

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.