Jump to content

Regex for pub id


spitfire1945

Recommended Posts

Hey folks. I am a little weak in the Regex field

 

I just need to validate google adsense publisher ids which come in a "pub-xxxxxxxxxxxxxxxx" (16 x's where all x's are numbers) format.

 

just the reg expression would be nice. Ill throw my guesses down though:

 

#$"pub-"^[0-9]{16}$#

 

??? am i even close?

Link to comment
Share on other sites

Why guess when you can simply try it out? Testing / experimenting with things can be quite interesting as well as admittedly sometimes frustrating (not to mention, most importantly, educational).

Taking self initiatives can go a long way towards independence.

Link to comment
Share on other sites

no i just simply fail at regex. It is probably the easiest and most robust concept but i cannot get it into my head.

 

Regex isn't necessarily hard, per say, but just slightly tricky at first. It's the rules of engagement that can kick your @$$ if you don't know what you are doing. You can have a look at the resources page as well as this tutorial from Darkwater. And of course, there is also this site as well as my favorite, this book.

 

Sorry for the link bombardment... there are plenty of options. As for regex itself, its just a matter of starting simple and building up. Overall, it's not hard, and quite nice to use once you really get the hang of it (almost addictive actually). I would need to attend a PCRE Anonymous meeting if there ever was such a thing...

Link to comment
Share on other sites

no i just simply fail at regex. It is probably the easiest and most robust concept but i cannot get it into my head.

 

Regex isn't necessarily hard, per say, but just slightly tricky at first. It's the rules of engagement that can kick your @$$ if you don't know what you are doing. You can have a look at the resources page as well as this tutorial from Darkwater. And of course, there is also this site as well as my favorite, this book.

 

Sorry for the link bombardment... there are plenty of options. As for regex itself, its just a matter of starting simple and building up. Overall, it's not hard, and quite nice to use once you really get the hang of it (almost addictive actually). I would need to attend a PCRE Anonymous meeting if there ever was such a thing...

 

Thanks alpha! I did look at those. I did have some success today when i was running the preg_match command for alphanumeric and underscore only strings.

 

preg_match("/[a-zA-Z0-9_]$/", $var) (and then i looked at ur signature and lol'ed)

 

it worked, i think i could have also used /w if i am not wrong.

Link to comment
Share on other sites

preg_match("/[a-zA-Z0-9_]$/", $var)

 

Alternatively, you could have done:

 

'/[a-z0-9_]$/i' <--------- the i modifier after the closing delimiter matches / captures case insensitive alpha characters

 

-or-

 

'/\w$/' <----------- the \w is a short hand character class for [a-zA-Z0-9_] ;)

Link to comment
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.