haku Posted November 19, 2009 Share Posted November 19, 2009 Regex is my weak point. I'm not so good at it. I just put this one together - can someone give it a lookover? It's supposed to match this pattern: * string starts with at least one digit, but could be multiple digits. No letters, only numbers * after the initial digits will be the string '_sign_in' * there will never be anything else - nothing between the digits and _sign_in, and _sign_in will always be the end of the string Here is what I have come up with: /^[0-9]+(_sign_in)$/ I think it's probably ok, but as I say, I'm not the best with regex. Can someone let me know if I've missed anything, or if it's ok? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/182111-check-my-regex/ Share on other sites More sharing options...
Adam Posted November 19, 2009 Share Posted November 19, 2009 Looks good to me, except unless you wish to capture the text "_sign_in" you don't need the brackets: /^[0-9]+_sign_in$/ Quote Link to comment https://forums.phpfreaks.com/topic/182111-check-my-regex/#findComment-960842 Share on other sites More sharing options...
thebadbad Posted November 19, 2009 Share Posted November 19, 2009 And just so you know, the dollar also matches immediately before the final character if it is a newline, unless you set the D modifier. So without setting it, a string like $str = "1_sign_in\n"; or $str = '1_sign_in '; //only on Linux/Unix would be matched by your pattern. Quote Link to comment https://forums.phpfreaks.com/topic/182111-check-my-regex/#findComment-960849 Share on other sites More sharing options...
haku Posted November 19, 2009 Author Share Posted November 19, 2009 Thanks guys - a couple of questions: Mr. Adam - what do you mean 'capture the text'? As in return it to the calling function? thebadbad - two questions. First, by 'set the D modifier', do you mean '/regex here/D'? Next, I'm checking a value pulled from the database that will never have a new line after it. I should be ok right? Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/182111-check-my-regex/#findComment-960856 Share on other sites More sharing options...
thebadbad Posted November 19, 2009 Share Posted November 19, 2009 A set of parentheses captures the match, as in stores it in the variable provided as the optional third parameter to preg_match() (often called $matches). And to your second question; yep, /pattern/D is right. If you're certain about the data (or if it doesn't matter) it's not necessary to use the D modifier, but I just tend to do it, when I want the dollar to match only at the very end of the string. Quote Link to comment https://forums.phpfreaks.com/topic/182111-check-my-regex/#findComment-960871 Share on other sites More sharing options...
haku Posted November 19, 2009 Author Share Posted November 19, 2009 Thanks - that clears it up for me. I actually bought a book on regex a couple of months ago, as I know it's something I need to brush up on, but I've just been too busy to work on it. Quote Link to comment https://forums.phpfreaks.com/topic/182111-check-my-regex/#findComment-960939 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.