PHP Developers Network
http://forums.devnetwork.net/

help a beginner
http://forums.devnetwork.net/viewtopic.php?f=38&t=137416
Page 1 of 1

Author:  quasiperfect [ Mon Feb 18, 2013 5:36 am ]
Post subject:  help a beginner

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 --

Author:  requinix [ Mon Feb 18, 2013 5:59 am ]
Post subject:  Re: help a beginner


Author:  quasiperfect [ Mon Feb 18, 2013 6:05 am ]
Post subject:  Re: help a beginner

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 --

Author:  s.dot [ Mon Feb 18, 2013 6:24 am ]
Post subject:  Re: help a beginner

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.

Author:  quasiperfect [ Mon Feb 18, 2013 6:29 am ]
Post subject:  Re: help a beginner

@s.dot tried that but won't catch double --

Author:  s.dot [ Mon Feb 18, 2013 7:43 am ]
Post subject:  Re: help a beginner


Author:  quasiperfect [ Mon Feb 18, 2013 7:56 am ]
Post subject:  Re: help a beginner

@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

Author:  requinix [ Mon Feb 18, 2013 9:17 am ]
Post subject:  Re: help a beginner


Author:  quasiperfect [ Mon Feb 18, 2013 9:56 am ]
Post subject:  Re: help a beginner

@requinix i solved the problem using a combination of regex and php functions but i wanted a full regex method

Author:  Christopher [ Mon Feb 18, 2013 2:01 pm ]
Post subject:  Re: help a beginner


Author:  requinix [ Mon Feb 18, 2013 8:03 pm ]
Post subject:  Re: help a beginner


Author:  Christopher [ Mon Feb 18, 2013 11:52 pm ]
Post subject:  Re: help a beginner

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?

Author:  regexer [ Sat Dec 20, 2014 8:01 pm ]
Post subject:  Re: help a beginner


Author:  phpRob [ Fri Feb 13, 2015 6:31 pm ]
Post subject:  Re: help a beginner

(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}(?<!\-)$/';
 

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/