M.O.S. Studios Posted October 10, 2010 Share Posted October 10, 2010 i using regex in a simple function. bascily i need to know if the value fits the following 2 criteria. 1. the first character is a + or - 2. the rest of the string is nothing but digits (only numbers, no decimals or other punctuation). this is what i tried ([+-]{1})([0-9]) the problem is it only checks the frst two characters, so this would pass '+1helloworld'. can any one help? Quote Link to comment https://forums.phpfreaks.com/topic/215534-so-lost-on-simply-regex-expression/ Share on other sites More sharing options...
Pikachu2000 Posted October 10, 2010 Share Posted October 10, 2010 Is the sign required, or is it safe to assume that if there is no negative sign, the number is positive? Quote Link to comment https://forums.phpfreaks.com/topic/215534-so-lost-on-simply-regex-expression/#findComment-1120799 Share on other sites More sharing options...
M.O.S. Studios Posted October 10, 2010 Author Share Posted October 10, 2010 Good question, it has to have one of three values, + then number, - then number, or jut a number. I was gong to use other means to check the latter. But now that I think of it it makes more scene to check all three right away. Quote Link to comment https://forums.phpfreaks.com/topic/215534-so-lost-on-simply-regex-expression/#findComment-1120856 Share on other sites More sharing options...
Adam Posted October 10, 2010 Share Posted October 10, 2010 You can use: ^(-|+)?[0-9]+$ Quote Link to comment https://forums.phpfreaks.com/topic/215534-so-lost-on-simply-regex-expression/#findComment-1120878 Share on other sites More sharing options...
M.O.S. Studios Posted October 11, 2010 Author Share Posted October 11, 2010 didn't work, but this did. ^([-+]|\d)((?![^0-9])[0-9])+$ for those of who found this while trying to do something similar I'll explain why. ^means see if the first fist this [-+]|\d. (brackets are added to mark what is included in the ^ command). [-+]|\d means the assigned search has to be + - or a number. then there is this. ((?![^0-9])[0-9])+$ (?![^0-9])[0-9]) means make sure there is no characters that are not between the numbers between 0 and 9. then +$ means start from the end and check until it is false. i hope this is clear. and if anyone see that i didn't explain it properly let me know! Quote Link to comment https://forums.phpfreaks.com/topic/215534-so-lost-on-simply-regex-expression/#findComment-1120973 Share on other sites More sharing options...
Adam Posted October 11, 2010 Share Posted October 11, 2010 My mistake, just forgot to escape the plus. Although can make it simpler, as you kind of did, by using block brackets for matching the plus/minus: var_dump(preg_match('/^[-+]?[0-9]+$/', '-123456')); // returns 1 The pattern you came up with though is just making it more complicated than it needs to be. Quote Link to comment https://forums.phpfreaks.com/topic/215534-so-lost-on-simply-regex-expression/#findComment-1120998 Share on other sites More sharing options...
salathe Posted October 11, 2010 Share Posted October 11, 2010 A simple alternative would be /^[-+]?\d+$/D which would match: one or more decimal digits optionally preceded by either a plus or a minus character. The D pattern modifier is used to disallow a newline (e.g. "12345\n") which the other suggested ones would allow. Quote Link to comment https://forums.phpfreaks.com/topic/215534-so-lost-on-simply-regex-expression/#findComment-1121013 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.