Solved

Split a sting into array

Posted on 2013-10-23
4
140 Views
Last Modified: 2013-11-07
Im faily new to regex and want to know if you can specify a start and end.

For example I have a statment:-

((x=1) AND ((X=3)OR(x=4)))

What I want to do is break it up into its 2 elements:-
(x=1)
((X=3)OR(x=4))

And then split the second into its elements:-
(X=3)
(x=4)

I also want to find out what's in-between each of the statements eg OR or AND

Is this even possible using regex?

Thank you
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39593676
With Php you could something like this:

$stmt = '((x=1) AND ((X=3)OR(x=4)))';

$andArray = explode('AND', $stmt);

$orArray = array();
foreach ($andArray as $arr){
  if (stristr($arr, 'OR')) {
    $orArray[] = explode('OR', $arr);
  }
}

Open in new window


This way you should get an array whose elements are divided by AND an array whose elements are divided by OR
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39593693
Using preg_split:

$stmt = '((x=1) AND ((X=3)OR(x=4)))';
$regex = '#(AND|OR)#';
$result = preg_split($regex, $stmt);
print_r($result); 

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39593696
Regex is about matching patterns of characters, and a pattern doesn't necessarily need to repeat. You simply need to specify the pattern (or template if you wish) that the regex engine should look for.

You have given a very specific example, but I suspect that the need for this will be much more generic. What are the rules for what you intend to match? Is it only "((x=1) AND ((X=3)OR(x=4)))", or are any parts of that string variable?


@marqusG

Your preg_split example does not satisfy the requirement:

I also want to find out what's in-between each of the statements eg OR or AND
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39593894
Yes, you're right, I forgot to add the flag:

$stmt = '((x=1) AND ((X=3)OR(x=4)))';
$regex = '#(AND|OR)#';
$result = preg_split($regex, $stmtm, PREG_SPLIT_DELIM_CAPTURE);
print_r($result); 

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question