Solved

Split a sting into array

Posted on 2013-10-23
4
135 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
  • 3
4 Comments
 
LVL 30

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 30

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 74

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 30

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…

759 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

21 Experts available now in Live!

Get 1:1 Help Now