PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 23, 2019 8:15 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: regex with pipes?
PostPosted: Mon Aug 05, 2013 10:32 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
I ran across some old code using regex like this for allowing alphanumeric with dot, dash and underscores:

Syntax: [ Download ] [ Hide ]
preg_match('|^[0-9.a-zA-Z_-]*$|', $value)


I was surprised to find it actually seemed to work, but I don't know why. The period isn't escaped and what's up with the pipes? I've only seen patterns done like /pattern/. I've found some cases where a similar regex from the same coder fails which makes me suspect they weren't properly tested. For example:

Syntax: [ Download ] [ Hide ]
preg_match('|[a-zA-Z]|', $value)


Seems to pass anything that has at least letter in it, but it is missing the string start ^ and $ so I would assume that it looks for one passing condition on any character and then returns a boolean result. The programmer was using this to validate alpha character strings, which obviously isn't correct, so I'm suspicious about all of the regex patterns.


Top
 Profile  
 
 Post subject: Re: regex with pipes?
PostPosted: Mon Aug 05, 2013 11:15 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
The delimiters, the slashes you're used to, don't actually have to be slashes. They can be pretty much any character you want - just make sure you have one at the beginning and at the end before any flags, and that you escape any uses of it inside the expression. / and # are most common or popular, but I've seen ! ~ | used too.

As for the period, the rules inside character sets change a bit: many metacharacters lose their special meaning. Like . + * ( ) { } $ all become just regular literal characters while ^ and - gain new/different meanings. So while you could escape that period if you wanted to, it's really not necessary because there's nothing to "escape".

Side note: if you want to validate a string containing only letters, is better.


Top
 Profile  
 
 Post subject: Re: regex with pipes?
PostPosted: Mon Aug 05, 2013 11:31 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Couldn't have said it better than requinix!

Other delimiters I like are tildes and commas --- but I may be alone on that one.
I'll add that the forward slash is one of the worst you could choose as a standard delimiter because sooner or later you'll want to match urls, which will give you this kind of soup:
$pattern = '/http:\/\/www.you.com\/pics\//';

In your character class, note that you have all the elements of \w: 0-9, a-z, A-Z, and underscore.
So you could streamline the pattern to '|^[-.\w]*$|'
Err, I meant, ',^[-.\w]*$,'
:wink:


Top
 Profile  
 
 Post subject: Re: regex with pipes?
PostPosted: Mon Aug 05, 2013 11:50 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
Thanks. I couldn't find any specs on the delimiters (at least on the php side of the documentation) and I didn't know that the character rules changed. Like I said this is some old code and I found a bug with the alpha filter and when I was looking deeper I saw these other odd regex patterns. Now I know. :)


Top
 Profile  
 
 Post subject: Re: regex with pipes?
PostPosted: Tue Aug 06, 2013 12:29 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
 Post subject: Re: regex with pipes?
PostPosted: Tue Aug 06, 2013 12:52 am 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group