PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat May 27, 2017 12:31 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: help a beginner
PostPosted: Mon Feb 18, 2013 5:36 am 
Offline
Forum Newbie

Joined: Mon Feb 18, 2013 5:30 am
Posts: 5
hi

i want to match strings with length min 1 max 10 characters composed by letters numbers and the character - (optional)
if present the character - can't be at start at end and don't allow double

and if is not to much please explain how you did it so i can understand and learn

thanks in advance for any help

edit

what i managed to do alone
^(?!-)[a-zA-Z0-9-]{1,10}(?<!-)$

but it doesn't catch double --


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 5:59 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6423
Location: WA, USA
So "it starts with at least one letter or number, then there's any amount of (hyphen and then at least one more letter or number)".

I recommend strlen() to check the length, then
Code:
/^[a-z0-9]+(-[a-z0-9]+)*$/i


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 6:05 am 
Offline
Forum Newbie

Joined: Mon Feb 18, 2013 5:30 am
Posts: 5
nope i will explain again
- it must contain at least one letter or number but no more then ten
- it can contain - but is optional , can't start with -, can't end with - and no double --


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 6:24 am 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
I'm by no means a regex guys. Please take my post as trying to help rather than it being the solution, although it might be. :) What if you try to match the - one or more times?

Such as:
Syntax: [ Download ] [ Hide ]
^(?!-+)[a-zA-Z0-9-]{1,10}(?<!-+)$


The + means one or more times.

_________________
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 6:29 am 
Offline
Forum Newbie

Joined: Mon Feb 18, 2013 5:30 am
Posts: 5
@s.dot tried that but won't catch double --


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 7:43 am 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
quasiperfect wrote:
@s.dot tried that but won't catch double --


Do you mean in the middle or at the beginning or end? If you mean in the midle, too.

Syntax: [ Download ] [ Hide ]
^(?!-+)[a-zA-Z0-9([^-]+)]{1,10}(?<!-+)$


That will be my last attempt because I'm literally just guessing. I am sorry. Hopefully someone more regex knowledgeable will chime in.

_________________
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 7:56 am 
Offline
Forum Newbie

Joined: Mon Feb 18, 2013 5:30 am
Posts: 5
@s.dot yes i mean in the middle too

i tried to use your regex but RegexBuddy 3 doesn't like it

# These errors were found in your original regular expression:
# Closing round bracket found without a corresponding opening round bracket

# Your regular expression could not be converted to the flavor required by this language:
# The PCRE library does not support infinite repetition inside lookbehind


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 9:17 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6423
Location: WA, USA
quasiperfect wrote:
nope i will explain again
- it must contain at least one letter or number but no more then ten
- it can contain - but is optional , can't start with -, can't end with - and no double --

Clearly my comment about strlen() and the expression I wrote have been of no help whatsoever.


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 9:56 am 
Offline
Forum Newbie

Joined: Mon Feb 18, 2013 5:30 am
Posts: 5
@requinix i solved the problem using a combination of regex and php functions but i wanted a full regex method


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 2:01 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13396
Location: New York, NY, US
quasiperfect wrote:
nope i will explain again
- it must contain at least one letter or number but no more then ten
- it can contain - but is optional , can't start with -, can't end with - and no double --

I don't think a single comprehensible regex will do that. I would recommend checking it step by step with clearly documented code. Probably a combination of regexs and looping through the string to do the counts. That is just too intricate to try to do in one shot.

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 8:03 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6423
Location: WA, USA
It is possible but I really don't like doing it. The trick is to use assertions.
Code:
/^(?=.{1,10}$)([a-z0-9](-[a-z0-9]+)*)$/i


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Mon Feb 18, 2013 11:52 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13396
Location: New York, NY, US
Yeah, I know. But does the average programmer really want to have to maintain that regex when the client asks for some tiny change in a year?

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Sat Dec 20, 2014 8:01 pm 
Offline
Forum Newbie

Joined: Sat Dec 20, 2014 7:38 pm
Posts: 4
Christopher wrote:
Yeah, I know. But does the average programmer really want to have to maintain that regex when the client asks for some tiny change in a year?

Unfortunately most programmers know only to write \d or .* in regexes .....

_________________
http://www.regex-builder.com/ online regular expression composer based on text analysis


Top
 Profile  
 
 Post subject: Re: help a beginner
PostPosted: Fri Feb 13, 2015 6:31 pm 
Offline
Forum Newbie

Joined: Thu Feb 12, 2015 4:45 pm
Posts: 9
(oops, I just solved it after writing a bunch...keeping it though due to some thoughts to consider)
I had been doing a good deal of regex lately and took a stab at your question. Kind of my version of a crossword puzzle. Not sure if you are doing your regex test on the Javascript- or the PHP-side. No matter really--yes, some syntaxes/techniques are not allowed on the js-side. If you are testing on the Javascript/client side, you could put two regex's in an onchange event-handler that returns true or false as to the success of a combination of two tests (provided below). If testing on the php side, testing with a method/function that uses string operations vs using a single statement regex test makes no matter of course, but I understand one would want the test to be robust yet concise just the same. I failed to enforce preventing multiple hyphens in a row without introducing a second test. Below, we want regExPattern1 to pass the test and regExPattern2 to fail the test. Sometimes dividing and conquering by breaking objectives down and solving on their own is the only way to go. Perhaps a single regex to test all objectives is possible; perhaps that single regex would be very difficult to maintain and modify without introducing a bug.
Syntax: [ Download ] [ Hide ]
$regExPattern1 = '/^[\da-zA-Z]{1}[\da-zA-Z\-]{0,9}(?<!\-)$/x';
$regExPattern2 = '/[\-]{2,}/';
 

This one solves what you asked for:
Syntax: [ Download ] [ Hide ]
$regExPattern = '/^[\da-zA-Z]{1}(?:[\d]|[a-z]|[A-Z]|(?<!\-)-){0,9}(?<!\-)$/';
 


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

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 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