Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/182111-check-my-regex/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182111-check-my-regex/#findComment-960849
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182111-check-my-regex/#findComment-960856
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182111-check-my-regex/#findComment-960871
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.