Jump to content

regex question


calabiyau

Recommended Posts

Okay I have a data field something like below

 

(100)+(100)++(200)===(100)++(200)++(200)==

 

there can be any number of + or = signs between blocks within the paranthesis.

 

I am stumped on how best to extract all the values into an array that are contained

within paranthesis.  so for above example array = "100,100,200,100,200,200"

 

I know I could do an series of explodes and multiple looping array routines to get to the

data but I thought there must be a regular expression solution to this.  I looked into

split and preg_split but can't come up with the regular expression that would represent

 

  ) plus any number of +== (

 

or perhaps there is someway to simply extract every value that occurs between ( ).

 

Anybody can help with this?

Link to comment
Share on other sites

If the number of numbers is vairable:

 

$val = '(100)+(100)++(200)===(100)++(200)++(200)==';

$res = print_r(preg_split('/\D+/', $val));

 

However,  it will give you, in this case, a null at the beginning and end of the array.

 

 

If the number of numbers is fixed:

 

preg_match(/\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)/, $val, $res);

 

Just don't forget to get rid of $res[0]

Link to comment
Share on other sites

THANK YOU!

 

Works beautifully on my limited dataset.

 

Just so I learn something here....what exactly does the /\D+/ part do, how is this representative

of the (100)+++(100), is it related to the  (100) or to the )++==(,  I see no indication of any of

the symbols, so is it a special instruction specially for things involving parenthesis, or some kind of

built in pattern seeker?

Link to comment
Share on other sites

preg_split //<< php regex function name....

( //<<< start the regex.....

' //<<< open the container

/ //<<< start regex code

\ \\<<<< use the charecter as it is defined

D \\ a defined charecter the char d means a digit

+ \\< plus add another together

/ \\<<<< close regex container

' \\<<<< close the regex

,\\<<< adding a comma for a varable or charecter or condition

$val \\ a varable

, \\ add a comma for condition or varable

null \\ added a condiriton of 0 but using the term NULL

,// add a comma for varable or condition

PREG_SPLIT_NO_EMPTY \\use the command to make sure regex not empty

)//close regex

;// tell php that this block of code stops here

 

<?php
preg_split('/\D+/', $val, null, PREG_SPLIT_NO_EMPTY);
?>

Link to comment
Share on other sites

I don't mean to speak down to you so please don't take it that way.  Explaining regular expressions just requires a lot of details when you don't know your audience.

 

The regex bestiary has some very powerful escape sequences...

 

escape  meaning

\d        a numbers (0-9)

\w        a word character (0-9, a-z, A-Z, _)

(and many many more)

 

when you use the caps version of these you get an "opposite" effect

 

escape  meaning

\D        not a number (anything but 0-9)

\W      not a word characters (anything but 0-9, a-z, A-Z, _)

(and many many more)

 

Greedy Modifiers:

+  One or more

*  Zero or more

?  Zero or One

 

 

Take it all together /\D+/ means match one or more not number characters. 

 

If you really want to have fun with regular expressions the escape sequences will give you the power you need to solve most problems.

For more see: (just search on \d to skip down to the escape sequences)

 

http://us2.php.net/manual/en/reference.pcre.pattern.syntax.php

 

 

Link to comment
Share on other sites

AHHHH...okay I think I got you.  So the fact that the data i'm seeking is only numbers allows me to use a very general statement to split it up by any sequence of characters that are not numbers.  fiendishly simple.  thanks to both of you.  i will look into this alot further.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.