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
https://forums.phpfreaks.com/topic/221135-help-with-regex/
Share on other sites

Thank you for your reply, I should have mentioned that the space in the expression may or may not be present.

You may have to trim some of the results:

 

$array = preg_split('/([<>=!]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);

Link to comment
https://forums.phpfreaks.com/topic/221135-help-with-regex/#findComment-1145061
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
https://forums.phpfreaks.com/topic/221135-help-with-regex/#findComment-1145126
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.