tjburke79 Posted July 26, 2011 Share Posted July 26, 2011 i am wondering if someone can help me out. i am trying to parse a field, and i need to split the field into 2 values. $Var = "+1.5-165"; or $Var = "-1.5+145"; i need +1.5 as on var and -165 as another, and it is possible that the numbers are switch around, so i can not use the explode function to spit them any body of a solution? thanks Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/ Share on other sites More sharing options...
gizmola Posted July 26, 2011 Share Posted July 26, 2011 I'm not sure what you mean by switched around. This is specific to the first number having a decimal and the second number not having one. However, preg_match will match both of these, but you'll have to figure out what elements in the match array to grab. Off the top of my head it would be match[3] and match[5] but you should var_dump and test. $regex = "/((\+|-)([0-9]+\.[0-9]+))((\+|-)[0-9]+)/"; preg_match($regex, $yourstring, $match); var_dump($match); Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1247533 Share on other sites More sharing options...
AyKay47 Posted July 26, 2011 Share Posted July 26, 2011 this can be done with a regex yes. $subject = "+1.5-165"; //or other way around $pattern = "~^(\+[0-9]+\.[0-9]+ |-[0-9]+\.[0-9]+)(\+\d+ |-[0-9]+)$~"; preg_match($pattern, $subject, $matches); foreach($matches as $match){ print "$match <br />"; } Edit: well I took the time to write it, so i might as well post it! Sorry Gizmo for the redundancy Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1247534 Share on other sites More sharing options...
xyph Posted July 26, 2011 Share Posted July 26, 2011 <?php $str = "+1.5-165"; $exp = "/[-+][0-9.]+/"; preg_match_all( $exp, $str, $matches ); print_r( $matches[0] ); ?> You guys use some COMPLEX regex Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1247535 Share on other sites More sharing options...
gizmola Posted July 26, 2011 Share Posted July 26, 2011 I like your xyph, although we don't know what might be in the input string, that looks good, and doesn't care about position. Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1247553 Share on other sites More sharing options...
xyph Posted July 26, 2011 Share Posted July 26, 2011 Just to note as well to AyKay - Your pattern breaks You have a space before your or pipes. Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1247556 Share on other sites More sharing options...
AyKay47 Posted July 27, 2011 Share Posted July 27, 2011 Just to note as well to AyKay - Your pattern breaks You have a space before your or pipes. \ahhh you're right, my fault I like your regex better anyways... Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1247694 Share on other sites More sharing options...
gizmola Posted July 27, 2011 Share Posted July 27, 2011 AyKay47, look into a testing tool like RegexCoach. You can verify your regex's do what you think they do without having to write up a test script. Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1247745 Share on other sites More sharing options...
AyKay47 Posted July 27, 2011 Share Posted July 27, 2011 AyKay47, look into a testing tool like RegexCoach. You can verify your regex's do what you think they do without having to write up a test script. yeah I was actually talking to xyph a little bit ago about the one that he uses..I will need to look into getting one, will save me some time and produce cleaner regex no doubt... Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1247857 Share on other sites More sharing options...
xyph Posted July 27, 2011 Share Posted July 27, 2011 RegEx Coach is free - Give it a shot first Quote Link to comment https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/#findComment-1248005 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.