Split a sting into array

Posted on 2013-10-23
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:-

And then split the second into its elements:-

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
Question by:tonelm54
  • 3
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
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);

Open in new window

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?


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
LVL 31

Accepted Solution

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);

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
plusOut java challenge 40 171
Regex to capture everything between two characters 6 80
regex code to filter this ip's? 2 34
regex expression 9 55
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now