Solved

Split a sting into array

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using SELECT with Expression.Lambda 13 88
Need Help to Improve the regular expression 3 59
SSRS - Suppress row when blank from 2nd dataset 2 64
RegEx with optional part 4 42
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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,…
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…

821 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