Jump to content

help with regex


syed

Recommended Posts

Hi guys, hope someone can help, Im having some problems spliting a string using the preg_split function. Basically im trying to split a string similar to this.

 

x > 1

Output needs to be array([0]=>x[1]=>>[2]=>1)

 

I need a regex that can also split on

 

x >= 1  or

x <= 1 or

x = 1 or

x != 1

 

Currently im using this, $regx = "/(<=|<|=|>|>=)/"; but for expressions like x >= 1  the returned array contains 2 elements one for each operator, I need just the one.

 

Any help will be greately appreciated.

Link to comment
Share on other sites

Hi Syed,

 

AbraCadaver has a pretty good solution but it can be improved upon, their solution will match "x < 1", "X <= 1" and "x <=! 1" etc...

 

I would do:

$array = preg_split('/([<>=!]{1,2})/', $string, null, PREG_SPLIT_DELIM_CAPTURE);

 

Hope that helps!

 

Muzzs

Link to comment
Share on other sites

Currently im using this, $regx = "/(<=|<|=|>|>=)/"; but for expressions like x >= 1  the returned array contains 2 elements one for each operator, I need just the one.

 

With that regex, >= will never get matched because > and = both occur individually before it in the group of alternatives.  Matching stops at the earliest successful match of an alternative.  If you moved >= earlier (at least, before = and >), then it would work fine just like <= does.

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.