Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
Hi experts,

this regex pattern
C#
string.Format("(?!.*(^[.-]|[.-]$|\\.\\.))(^[a-zA-Z0-9.-]{{{0},{1}}}$)", minLength, maxLength);
works for
- upper- and lowercase letters plus period and dash
- no period nor dash at start or end
- no double periods
- length between given min and max

Now how do I say in RegEx "If there is \., then between it and $, there may exist [a-zA-Z-]+ (but no [0-9] anymore)?"


[Edit3]
I did some more research and came up with this pattern:
C#
string enrichedPattern = string.Format(
    "((?!^-)(^([a-zA-Z0-9-]+\\.)*([a-zA-Z]+)$)){{{0},{1}}}",
    minLength, maxLength
);
Ignoring C#'s overhead to the pattern, it becomes
((?!^-)(^([a-zA-Z0-9-]+\.)*([a-zA-Z]+)$)){MinLength,MaxLength}
What it should do:
There are four parts.
1) (?!^-)(whatever)
2) ([a-zA-Z0-9-]+\.)*
3) ([a-zA-Z]+)$
4) (whatever){MinLength,MaxLength}

3) Is the most important part since it is the only mandatory one. It contains alphas only. It can be the only part or be preceded by an instance of 2) separated by a period.
Examples: MyDoMaIn.

2) Contains alphanumerics and dashes. It can extend 1) by more groups, separated through periods.
Examples: MyDoMaIn22.com or sub.domain

1) forbids to start with a dash. So no -sub.domain any more. Make it sub.domain or yellow.-sub.domain

4) limits the overall length to between MinLength and MaxLength

And here is my problem: As long as MinLength is at least one, the forementioned works as expected (or I haven't found a negative example yet).
But with a minimum value of zero, it seems that everything is allowed. Periods/dashes at start and end, numbers in the last part, even number-only stuff.

How does that RegEx thing work that with a minimum length of zero, it lets everything pass?
And how can I get around that, so that even if zero length is allowed, the rules are applied if there is something to match the pattern against?
[/Edit3]
Posted
Updated 1-Jul-13 21:05pm
v4
Comments
CHill60 27-Jun-13 15:44pm    
I'm not the best at Regex but here's a link I've used before that might have what you need - warning some of the regex's don't work, but luckily there is a tester there so you can have a play ... http://regexlib.com/RETester.aspx[^]
lukeer 28-Jun-13 8:38am    
I elaborated at my question. Can you have a look at the new situation?
lukeer 11-Jul-13 7:25am    
Thanks, I found this local RegEx tester to be a very helpful tool and made up a solution.

1 solution

After some trial and reading, I found that one worked:
C#
Pattern = string.Format("^(([A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])*\\.)*[A-Za-z]+){{{0},{1}}}$", minLength, maxLength);
No more explicit prohibition of character combinations and yet it works:
The last part (with "{{" and such) defines the allowed length.
Prior to that is the mandatory alpha-only part.
It can be preceded by no or many optional parts.

Within an optional part, there is one mandatory again. Which means "If there is something before the global mandatory part, there has to be an alpha and a period".
Between alpha and period there can be alphaNums. And between those alphaNums, if they exist, there can be alphaNumDashes.

And the problem regarding zero-length inputs is sorted out by placing the start^ and end$ at slightly different positions. That's something I don't understand the reasons for, but it tested Ok.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900