Page 1 of 1

help a beginner

Posted: Mon Feb 18, 2013 4:36 am
by quasiperfect
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 --

Re: help a beginner

Posted: Mon Feb 18, 2013 4:59 am
by requinix
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: Select all

/^[a-z0-9]+(-[a-z0-9]+)*$/i

Re: help a beginner

Posted: Mon Feb 18, 2013 5:05 am
by quasiperfect
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 --

Re: help a beginner

Posted: Mon Feb 18, 2013 5:24 am
by s.dot
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:
[text]^(?!-+)[a-zA-Z0-9-]{1,10}(?<!-+)$[/text]

The + means one or more times.

Re: help a beginner

Posted: Mon Feb 18, 2013 5:29 am
by quasiperfect
@s.dot tried that but won't catch double --

Re: help a beginner

Posted: Mon Feb 18, 2013 6:43 am
by s.dot
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.

[text]^(?!-+)[a-zA-Z0-9([^-]+)]{1,10}(?<!-+)$[/text]

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

Re: help a beginner

Posted: Mon Feb 18, 2013 6:56 am
by quasiperfect
@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

Re: help a beginner

Posted: Mon Feb 18, 2013 8:17 am
by requinix
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.

Re: help a beginner

Posted: Mon Feb 18, 2013 8:56 am
by quasiperfect
@requinix i solved the problem using a combination of regex and php functions but i wanted a full regex method

Re: help a beginner

Posted: Mon Feb 18, 2013 1:01 pm
by Christopher
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.

Re: help a beginner

Posted: Mon Feb 18, 2013 7:03 pm
by requinix
It is possible but I really don't like doing it. The trick is to use assertions.

Code: Select all

/^(?=.{1,10}$)([a-z0-9](-[a-z0-9]+)*)$/i

Re: help a beginner

Posted: Mon Feb 18, 2013 10:52 pm
by Christopher
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?

Re: help a beginner

Posted: Sat Dec 20, 2014 7:01 pm
by regexer
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 .....

Re: help a beginner

Posted: Fri Feb 13, 2015 5:31 pm
by phpRob
(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.

Code: Select all

$regExPattern1 = '/^[\da-zA-Z]{1}[\da-zA-Z\-]{0,9}(?<!\-)$/x';
$regExPattern2 = '/[\-]{2,}/';
This one solves what you asked for:

Code: Select all

$regExPattern = '/^[\da-zA-Z]{1}(?:[\d]|[a-z]|[A-Z]|(?<!\-)-){0,9}(?<!\-)$/';