help a beginner
Moderator: General Moderators
-
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 18, 2013 4:30 am
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 --
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
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
I recommend strlen() to check the length, then
Code: Select all
/^[a-z0-9]+(-[a-z0-9]+)*$/i
-
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 18, 2013 4:30 am
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 --
- 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
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.

Such as:
[text]^(?!-+)[a-zA-Z0-9-]{1,10}(?<!-+)$[/text]
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.
-
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 18, 2013 4:30 am
Re: help a beginner
@s.dot tried that but won't catch double --
Re: help a beginner
Do you mean in the middle or at the beginning or end? If you mean in the midle, too.quasiperfect wrote:@s.dot tried that but won't catch double --
[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.
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.
-
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 18, 2013 4:30 am
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
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
Clearly my comment about strlen() and the expression I wrote have been of no help whatsoever.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 --
-
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 18, 2013 4:30 am
Re: help a beginner
@requinix i solved the problem using a combination of regex and php functions but i wanted a full regex method
- Christopher
- Site Administrator
- Posts: 13592
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: help a beginner
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.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 --
(#10850)
Re: help a beginner
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
- Christopher
- Site Administrator
- Posts: 13592
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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?
(#10850)
Re: help a beginner
Unfortunately most programmers know only to write \d or .* in regexes .....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?
http://www.regex-builder.com/ online regular expression composer based on text analysis
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.
This one solves what you asked for:
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,}/';
Code: Select all
$regExPattern = '/^[\da-zA-Z]{1}(?:[\d]|[a-z]|[A-Z]|(?<!\-)-){0,9}(?<!\-)$/';