Regex for a string with at least a letter and no repeating dash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I had a problem a few days back and had to find a regex that matches a string that:



  • Contains only letters in the alphabet [a-z0-9-] so lowercase latin letters, numbers 0 to 9, and the dash character

  • Must contains at least a letter [a-z]

  • Must not contains repeating dashes. abc-def-gh is ok but not abc--def

  • The size of the string must be between 1 and 10 characters

I came up with:



^(?=[a-z0-9-]*[a-z][a-z0-9-]*)(?:[a-z0-9]|[-](?![-]))1,10$


A little explanation:




  • (?=[a-z0-9-]*[a-z][a-z0-9-]*) lookahead to find if the string contains at least a letter


  • (?:[a-z0-9]|[-](?![-]))1,10 A non capturing group (not really important to capture or not I believe) with two parts, that must be between 1 and 10 chars


    • [a-z0-9] any letter in the alphabet I want


    • [-](?![-] a dash then a negative lookahead to see if there is no dash following


Here is a list of strings that should not match:



#should not match
-
1
1-
-1
aaaaaaaaaaaaaaaaa
11111
2a2--af
a2a--22


And a list of string that should match:



#should match
a
a-
-a
a-2
a2a
2a2-
a2a-2a2
a-b-c-d
a123213232
123213213a
a12321322a


HERE you can find an online regex tester to play with it.



All improvements, remarks, feedbacks is welcome. My guts is telling me the lookahead can be improved but I did not find how. Also, I'm not even sure the lookahead is needed at all. I'm eager to improve my regex skill so if you have any alternative method also I'd be really glad to know.







share|improve this question

















  • 1




    by "Must not contain repeating dashes", do you mean "no more than one dash total" or "no consecutive dashes"? i.e. should it match abc-def-ghi? the current regex doesn't match that.
    – cas
    Jan 13 at 10:34










  • Yes I mean no a--a but abc-chd-dhd should work
    – Julien Rousé
    Jan 13 at 10:37










  • In the regex tester, I believe it matches abc-def-gh? It doesn't work with abc-def-ghi because it's too long and the string should be 10 maximum. see here at the bottom
    – Julien Rousé
    Jan 13 at 10:43






  • 1




    doh, you're right. i didn't think to count the chars, and typed too fast to notice it change when i typed the 10th character.
    – cas
    Jan 13 at 10:44










  • No worries, thanks for your input! And in the comment I told you were right when it wasn't, I didn't check either if abc-def-ghi was too long, my mistake.
    – Julien Rousé
    Jan 13 at 10:45

















up vote
1
down vote

favorite












I had a problem a few days back and had to find a regex that matches a string that:



  • Contains only letters in the alphabet [a-z0-9-] so lowercase latin letters, numbers 0 to 9, and the dash character

  • Must contains at least a letter [a-z]

  • Must not contains repeating dashes. abc-def-gh is ok but not abc--def

  • The size of the string must be between 1 and 10 characters

I came up with:



^(?=[a-z0-9-]*[a-z][a-z0-9-]*)(?:[a-z0-9]|[-](?![-]))1,10$


A little explanation:




  • (?=[a-z0-9-]*[a-z][a-z0-9-]*) lookahead to find if the string contains at least a letter


  • (?:[a-z0-9]|[-](?![-]))1,10 A non capturing group (not really important to capture or not I believe) with two parts, that must be between 1 and 10 chars


    • [a-z0-9] any letter in the alphabet I want


    • [-](?![-] a dash then a negative lookahead to see if there is no dash following


Here is a list of strings that should not match:



#should not match
-
1
1-
-1
aaaaaaaaaaaaaaaaa
11111
2a2--af
a2a--22


And a list of string that should match:



#should match
a
a-
-a
a-2
a2a
2a2-
a2a-2a2
a-b-c-d
a123213232
123213213a
a12321322a


HERE you can find an online regex tester to play with it.



All improvements, remarks, feedbacks is welcome. My guts is telling me the lookahead can be improved but I did not find how. Also, I'm not even sure the lookahead is needed at all. I'm eager to improve my regex skill so if you have any alternative method also I'd be really glad to know.







share|improve this question

















  • 1




    by "Must not contain repeating dashes", do you mean "no more than one dash total" or "no consecutive dashes"? i.e. should it match abc-def-ghi? the current regex doesn't match that.
    – cas
    Jan 13 at 10:34










  • Yes I mean no a--a but abc-chd-dhd should work
    – Julien Rousé
    Jan 13 at 10:37










  • In the regex tester, I believe it matches abc-def-gh? It doesn't work with abc-def-ghi because it's too long and the string should be 10 maximum. see here at the bottom
    – Julien Rousé
    Jan 13 at 10:43






  • 1




    doh, you're right. i didn't think to count the chars, and typed too fast to notice it change when i typed the 10th character.
    – cas
    Jan 13 at 10:44










  • No worries, thanks for your input! And in the comment I told you were right when it wasn't, I didn't check either if abc-def-ghi was too long, my mistake.
    – Julien Rousé
    Jan 13 at 10:45













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I had a problem a few days back and had to find a regex that matches a string that:



  • Contains only letters in the alphabet [a-z0-9-] so lowercase latin letters, numbers 0 to 9, and the dash character

  • Must contains at least a letter [a-z]

  • Must not contains repeating dashes. abc-def-gh is ok but not abc--def

  • The size of the string must be between 1 and 10 characters

I came up with:



^(?=[a-z0-9-]*[a-z][a-z0-9-]*)(?:[a-z0-9]|[-](?![-]))1,10$


A little explanation:




  • (?=[a-z0-9-]*[a-z][a-z0-9-]*) lookahead to find if the string contains at least a letter


  • (?:[a-z0-9]|[-](?![-]))1,10 A non capturing group (not really important to capture or not I believe) with two parts, that must be between 1 and 10 chars


    • [a-z0-9] any letter in the alphabet I want


    • [-](?![-] a dash then a negative lookahead to see if there is no dash following


Here is a list of strings that should not match:



#should not match
-
1
1-
-1
aaaaaaaaaaaaaaaaa
11111
2a2--af
a2a--22


And a list of string that should match:



#should match
a
a-
-a
a-2
a2a
2a2-
a2a-2a2
a-b-c-d
a123213232
123213213a
a12321322a


HERE you can find an online regex tester to play with it.



All improvements, remarks, feedbacks is welcome. My guts is telling me the lookahead can be improved but I did not find how. Also, I'm not even sure the lookahead is needed at all. I'm eager to improve my regex skill so if you have any alternative method also I'd be really glad to know.







share|improve this question













I had a problem a few days back and had to find a regex that matches a string that:



  • Contains only letters in the alphabet [a-z0-9-] so lowercase latin letters, numbers 0 to 9, and the dash character

  • Must contains at least a letter [a-z]

  • Must not contains repeating dashes. abc-def-gh is ok but not abc--def

  • The size of the string must be between 1 and 10 characters

I came up with:



^(?=[a-z0-9-]*[a-z][a-z0-9-]*)(?:[a-z0-9]|[-](?![-]))1,10$


A little explanation:




  • (?=[a-z0-9-]*[a-z][a-z0-9-]*) lookahead to find if the string contains at least a letter


  • (?:[a-z0-9]|[-](?![-]))1,10 A non capturing group (not really important to capture or not I believe) with two parts, that must be between 1 and 10 chars


    • [a-z0-9] any letter in the alphabet I want


    • [-](?![-] a dash then a negative lookahead to see if there is no dash following


Here is a list of strings that should not match:



#should not match
-
1
1-
-1
aaaaaaaaaaaaaaaaa
11111
2a2--af
a2a--22


And a list of string that should match:



#should match
a
a-
-a
a-2
a2a
2a2-
a2a-2a2
a-b-c-d
a123213232
123213213a
a12321322a


HERE you can find an online regex tester to play with it.



All improvements, remarks, feedbacks is welcome. My guts is telling me the lookahead can be improved but I did not find how. Also, I'm not even sure the lookahead is needed at all. I'm eager to improve my regex skill so if you have any alternative method also I'd be really glad to know.









share|improve this question












share|improve this question




share|improve this question








edited Jan 14 at 23:15









Jamal♦

30.1k11114225




30.1k11114225









asked Jan 13 at 10:07









Julien Rousé

446416




446416







  • 1




    by "Must not contain repeating dashes", do you mean "no more than one dash total" or "no consecutive dashes"? i.e. should it match abc-def-ghi? the current regex doesn't match that.
    – cas
    Jan 13 at 10:34










  • Yes I mean no a--a but abc-chd-dhd should work
    – Julien Rousé
    Jan 13 at 10:37










  • In the regex tester, I believe it matches abc-def-gh? It doesn't work with abc-def-ghi because it's too long and the string should be 10 maximum. see here at the bottom
    – Julien Rousé
    Jan 13 at 10:43






  • 1




    doh, you're right. i didn't think to count the chars, and typed too fast to notice it change when i typed the 10th character.
    – cas
    Jan 13 at 10:44










  • No worries, thanks for your input! And in the comment I told you were right when it wasn't, I didn't check either if abc-def-ghi was too long, my mistake.
    – Julien Rousé
    Jan 13 at 10:45













  • 1




    by "Must not contain repeating dashes", do you mean "no more than one dash total" or "no consecutive dashes"? i.e. should it match abc-def-ghi? the current regex doesn't match that.
    – cas
    Jan 13 at 10:34










  • Yes I mean no a--a but abc-chd-dhd should work
    – Julien Rousé
    Jan 13 at 10:37










  • In the regex tester, I believe it matches abc-def-gh? It doesn't work with abc-def-ghi because it's too long and the string should be 10 maximum. see here at the bottom
    – Julien Rousé
    Jan 13 at 10:43






  • 1




    doh, you're right. i didn't think to count the chars, and typed too fast to notice it change when i typed the 10th character.
    – cas
    Jan 13 at 10:44










  • No worries, thanks for your input! And in the comment I told you were right when it wasn't, I didn't check either if abc-def-ghi was too long, my mistake.
    – Julien Rousé
    Jan 13 at 10:45








1




1




by "Must not contain repeating dashes", do you mean "no more than one dash total" or "no consecutive dashes"? i.e. should it match abc-def-ghi? the current regex doesn't match that.
– cas
Jan 13 at 10:34




by "Must not contain repeating dashes", do you mean "no more than one dash total" or "no consecutive dashes"? i.e. should it match abc-def-ghi? the current regex doesn't match that.
– cas
Jan 13 at 10:34












Yes I mean no a--a but abc-chd-dhd should work
– Julien Rousé
Jan 13 at 10:37




Yes I mean no a--a but abc-chd-dhd should work
– Julien Rousé
Jan 13 at 10:37












In the regex tester, I believe it matches abc-def-gh? It doesn't work with abc-def-ghi because it's too long and the string should be 10 maximum. see here at the bottom
– Julien Rousé
Jan 13 at 10:43




In the regex tester, I believe it matches abc-def-gh? It doesn't work with abc-def-ghi because it's too long and the string should be 10 maximum. see here at the bottom
– Julien Rousé
Jan 13 at 10:43




1




1




doh, you're right. i didn't think to count the chars, and typed too fast to notice it change when i typed the 10th character.
– cas
Jan 13 at 10:44




doh, you're right. i didn't think to count the chars, and typed too fast to notice it change when i typed the 10th character.
– cas
Jan 13 at 10:44












No worries, thanks for your input! And in the comment I told you were right when it wasn't, I didn't check either if abc-def-ghi was too long, my mistake.
– Julien Rousé
Jan 13 at 10:45





No worries, thanks for your input! And in the comment I told you were right when it wasn't, I didn't check either if abc-def-ghi was too long, my mistake.
– Julien Rousé
Jan 13 at 10:45











1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The lookahead (?=[a-z0-9-]*[a-z][a-z0-9-]*) can be reduce to:



(?=.*[a-z])


because the allowed characters are defined after in the non capturing group.



The dash [-] doesn't need to be inside a character class, - is enough



Then, the whole regex becomes:



^(?=.*[a-z])(?:[a-z0-9]|-(?!-))1,10$





share|improve this answer





















  • Thank you for your help! Was there any way to get rid of that lookahead?
    – Julien Rousé
    Jan 13 at 11:22






  • 1




    @JulienRousé: No, it is the simplest way to do such checking.
    – Toto
    Jan 13 at 11:38










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185024%2fregex-for-a-string-with-at-least-a-letter-and-no-repeating-dash%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The lookahead (?=[a-z0-9-]*[a-z][a-z0-9-]*) can be reduce to:



(?=.*[a-z])


because the allowed characters are defined after in the non capturing group.



The dash [-] doesn't need to be inside a character class, - is enough



Then, the whole regex becomes:



^(?=.*[a-z])(?:[a-z0-9]|-(?!-))1,10$





share|improve this answer





















  • Thank you for your help! Was there any way to get rid of that lookahead?
    – Julien Rousé
    Jan 13 at 11:22






  • 1




    @JulienRousé: No, it is the simplest way to do such checking.
    – Toto
    Jan 13 at 11:38














up vote
1
down vote



accepted










The lookahead (?=[a-z0-9-]*[a-z][a-z0-9-]*) can be reduce to:



(?=.*[a-z])


because the allowed characters are defined after in the non capturing group.



The dash [-] doesn't need to be inside a character class, - is enough



Then, the whole regex becomes:



^(?=.*[a-z])(?:[a-z0-9]|-(?!-))1,10$





share|improve this answer





















  • Thank you for your help! Was there any way to get rid of that lookahead?
    – Julien Rousé
    Jan 13 at 11:22






  • 1




    @JulienRousé: No, it is the simplest way to do such checking.
    – Toto
    Jan 13 at 11:38












up vote
1
down vote



accepted







up vote
1
down vote



accepted






The lookahead (?=[a-z0-9-]*[a-z][a-z0-9-]*) can be reduce to:



(?=.*[a-z])


because the allowed characters are defined after in the non capturing group.



The dash [-] doesn't need to be inside a character class, - is enough



Then, the whole regex becomes:



^(?=.*[a-z])(?:[a-z0-9]|-(?!-))1,10$





share|improve this answer













The lookahead (?=[a-z0-9-]*[a-z][a-z0-9-]*) can be reduce to:



(?=.*[a-z])


because the allowed characters are defined after in the non capturing group.



The dash [-] doesn't need to be inside a character class, - is enough



Then, the whole regex becomes:



^(?=.*[a-z])(?:[a-z0-9]|-(?!-))1,10$






share|improve this answer













share|improve this answer



share|improve this answer











answered Jan 13 at 10:34









Toto

5391613




5391613











  • Thank you for your help! Was there any way to get rid of that lookahead?
    – Julien Rousé
    Jan 13 at 11:22






  • 1




    @JulienRousé: No, it is the simplest way to do such checking.
    – Toto
    Jan 13 at 11:38
















  • Thank you for your help! Was there any way to get rid of that lookahead?
    – Julien Rousé
    Jan 13 at 11:22






  • 1




    @JulienRousé: No, it is the simplest way to do such checking.
    – Toto
    Jan 13 at 11:38















Thank you for your help! Was there any way to get rid of that lookahead?
– Julien Rousé
Jan 13 at 11:22




Thank you for your help! Was there any way to get rid of that lookahead?
– Julien Rousé
Jan 13 at 11:22




1




1




@JulienRousé: No, it is the simplest way to do such checking.
– Toto
Jan 13 at 11:38




@JulienRousé: No, it is the simplest way to do such checking.
– Toto
Jan 13 at 11:38












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185024%2fregex-for-a-string-with-at-least-a-letter-and-no-repeating-dash%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?