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
https://forums.phpfreaks.com/topic/146017-regex-for-pub-id/
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
https://forums.phpfreaks.com/topic/146017-regex-for-pub-id/#findComment-766632
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
https://forums.phpfreaks.com/topic/146017-regex-for-pub-id/#findComment-767821
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
https://forums.phpfreaks.com/topic/146017-regex-for-pub-id/#findComment-768269
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
https://forums.phpfreaks.com/topic/146017-regex-for-pub-id/#findComment-768475
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.