Regular expression for letters, spaces and hyphens





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Looking for the regex to allow letters (either case), spaces and dashes for validation in ruby. Can't quite crack it.



As a starting point I'm using:



validates :name, format: { with: /A[a-zA-Z]+(?: [a-zA-Z]+)?z/, allow_blank: true}


Many thanks!










share|improve this question




















  • 3





    Welcome to SO. I suspect this question has been downvoted because it is unclear what you're asking for. I can recommend rubular.com as a resource, tho. You can use the key for parts of regexes in ruby (there are different flavours of regex in different language), and try it out for yourself.

    – AJFaraday
    Jan 4 at 13:51






  • 3





    /A[-A-Zs]+z/i would be enough.

    – Aleksei Matiushkin
    Jan 4 at 13:51








  • 3





    Also, is “λ” a letter? Is “я” a letter? Is “ä” a letter?

    – Aleksei Matiushkin
    Jan 4 at 13:52






  • 4





    If you need to support all Unicode letters, make sure - and spaces only appear between letters and no consecutive spaces/hyphens may occur (and there may be any amount of spaces/hyphens), use /Ap{L}+(?:[- ]p{L}+)*z/

    – Wiktor Stribiżew
    Jan 4 at 14:00








  • 1





    Of the answers and comments so far, only @Wiktor gets it right, because there is no requirement that whitespace be permitted, only spaces. Suppose str = "anb". Then str.match? /A[-A-Zs]+z/i #=> true, which is incorrect, whereas str.match? /Ap{L}+(?:[- ]p{L}+)*z/ #=> false. The former can be limited to spaces by simply changing s to a space. I see this mistake time and again.

    – Cary Swoveland
    Jan 4 at 17:53




















1















Looking for the regex to allow letters (either case), spaces and dashes for validation in ruby. Can't quite crack it.



As a starting point I'm using:



validates :name, format: { with: /A[a-zA-Z]+(?: [a-zA-Z]+)?z/, allow_blank: true}


Many thanks!










share|improve this question




















  • 3





    Welcome to SO. I suspect this question has been downvoted because it is unclear what you're asking for. I can recommend rubular.com as a resource, tho. You can use the key for parts of regexes in ruby (there are different flavours of regex in different language), and try it out for yourself.

    – AJFaraday
    Jan 4 at 13:51






  • 3





    /A[-A-Zs]+z/i would be enough.

    – Aleksei Matiushkin
    Jan 4 at 13:51








  • 3





    Also, is “λ” a letter? Is “я” a letter? Is “ä” a letter?

    – Aleksei Matiushkin
    Jan 4 at 13:52






  • 4





    If you need to support all Unicode letters, make sure - and spaces only appear between letters and no consecutive spaces/hyphens may occur (and there may be any amount of spaces/hyphens), use /Ap{L}+(?:[- ]p{L}+)*z/

    – Wiktor Stribiżew
    Jan 4 at 14:00








  • 1





    Of the answers and comments so far, only @Wiktor gets it right, because there is no requirement that whitespace be permitted, only spaces. Suppose str = "anb". Then str.match? /A[-A-Zs]+z/i #=> true, which is incorrect, whereas str.match? /Ap{L}+(?:[- ]p{L}+)*z/ #=> false. The former can be limited to spaces by simply changing s to a space. I see this mistake time and again.

    – Cary Swoveland
    Jan 4 at 17:53
















1












1








1








Looking for the regex to allow letters (either case), spaces and dashes for validation in ruby. Can't quite crack it.



As a starting point I'm using:



validates :name, format: { with: /A[a-zA-Z]+(?: [a-zA-Z]+)?z/, allow_blank: true}


Many thanks!










share|improve this question
















Looking for the regex to allow letters (either case), spaces and dashes for validation in ruby. Can't quite crack it.



As a starting point I'm using:



validates :name, format: { with: /A[a-zA-Z]+(?: [a-zA-Z]+)?z/, allow_blank: true}


Many thanks!







ruby regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 13:50









Aleksei Matiushkin

84.9k95896




84.9k95896










asked Jan 4 at 13:47









bidgeeboybidgeeboy

132




132








  • 3





    Welcome to SO. I suspect this question has been downvoted because it is unclear what you're asking for. I can recommend rubular.com as a resource, tho. You can use the key for parts of regexes in ruby (there are different flavours of regex in different language), and try it out for yourself.

    – AJFaraday
    Jan 4 at 13:51






  • 3





    /A[-A-Zs]+z/i would be enough.

    – Aleksei Matiushkin
    Jan 4 at 13:51








  • 3





    Also, is “λ” a letter? Is “я” a letter? Is “ä” a letter?

    – Aleksei Matiushkin
    Jan 4 at 13:52






  • 4





    If you need to support all Unicode letters, make sure - and spaces only appear between letters and no consecutive spaces/hyphens may occur (and there may be any amount of spaces/hyphens), use /Ap{L}+(?:[- ]p{L}+)*z/

    – Wiktor Stribiżew
    Jan 4 at 14:00








  • 1





    Of the answers and comments so far, only @Wiktor gets it right, because there is no requirement that whitespace be permitted, only spaces. Suppose str = "anb". Then str.match? /A[-A-Zs]+z/i #=> true, which is incorrect, whereas str.match? /Ap{L}+(?:[- ]p{L}+)*z/ #=> false. The former can be limited to spaces by simply changing s to a space. I see this mistake time and again.

    – Cary Swoveland
    Jan 4 at 17:53
















  • 3





    Welcome to SO. I suspect this question has been downvoted because it is unclear what you're asking for. I can recommend rubular.com as a resource, tho. You can use the key for parts of regexes in ruby (there are different flavours of regex in different language), and try it out for yourself.

    – AJFaraday
    Jan 4 at 13:51






  • 3





    /A[-A-Zs]+z/i would be enough.

    – Aleksei Matiushkin
    Jan 4 at 13:51








  • 3





    Also, is “λ” a letter? Is “я” a letter? Is “ä” a letter?

    – Aleksei Matiushkin
    Jan 4 at 13:52






  • 4





    If you need to support all Unicode letters, make sure - and spaces only appear between letters and no consecutive spaces/hyphens may occur (and there may be any amount of spaces/hyphens), use /Ap{L}+(?:[- ]p{L}+)*z/

    – Wiktor Stribiżew
    Jan 4 at 14:00








  • 1





    Of the answers and comments so far, only @Wiktor gets it right, because there is no requirement that whitespace be permitted, only spaces. Suppose str = "anb". Then str.match? /A[-A-Zs]+z/i #=> true, which is incorrect, whereas str.match? /Ap{L}+(?:[- ]p{L}+)*z/ #=> false. The former can be limited to spaces by simply changing s to a space. I see this mistake time and again.

    – Cary Swoveland
    Jan 4 at 17:53










3




3





Welcome to SO. I suspect this question has been downvoted because it is unclear what you're asking for. I can recommend rubular.com as a resource, tho. You can use the key for parts of regexes in ruby (there are different flavours of regex in different language), and try it out for yourself.

– AJFaraday
Jan 4 at 13:51





Welcome to SO. I suspect this question has been downvoted because it is unclear what you're asking for. I can recommend rubular.com as a resource, tho. You can use the key for parts of regexes in ruby (there are different flavours of regex in different language), and try it out for yourself.

– AJFaraday
Jan 4 at 13:51




3




3





/A[-A-Zs]+z/i would be enough.

– Aleksei Matiushkin
Jan 4 at 13:51







/A[-A-Zs]+z/i would be enough.

– Aleksei Matiushkin
Jan 4 at 13:51






3




3





Also, is “λ” a letter? Is “я” a letter? Is “ä” a letter?

– Aleksei Matiushkin
Jan 4 at 13:52





Also, is “λ” a letter? Is “я” a letter? Is “ä” a letter?

– Aleksei Matiushkin
Jan 4 at 13:52




4




4





If you need to support all Unicode letters, make sure - and spaces only appear between letters and no consecutive spaces/hyphens may occur (and there may be any amount of spaces/hyphens), use /Ap{L}+(?:[- ]p{L}+)*z/

– Wiktor Stribiżew
Jan 4 at 14:00







If you need to support all Unicode letters, make sure - and spaces only appear between letters and no consecutive spaces/hyphens may occur (and there may be any amount of spaces/hyphens), use /Ap{L}+(?:[- ]p{L}+)*z/

– Wiktor Stribiżew
Jan 4 at 14:00






1




1





Of the answers and comments so far, only @Wiktor gets it right, because there is no requirement that whitespace be permitted, only spaces. Suppose str = "anb". Then str.match? /A[-A-Zs]+z/i #=> true, which is incorrect, whereas str.match? /Ap{L}+(?:[- ]p{L}+)*z/ #=> false. The former can be limited to spaces by simply changing s to a space. I see this mistake time and again.

– Cary Swoveland
Jan 4 at 17:53







Of the answers and comments so far, only @Wiktor gets it right, because there is no requirement that whitespace be permitted, only spaces. Suppose str = "anb". Then str.match? /A[-A-Zs]+z/i #=> true, which is incorrect, whereas str.match? /Ap{L}+(?:[- ]p{L}+)*z/ #=> false. The former can be limited to spaces by simply changing s to a space. I see this mistake time and again.

– Cary Swoveland
Jan 4 at 17:53














1 Answer
1






active

oldest

votes


















0














This regex will allow letters, spaces and hyphens: /^[A-Za-zs-]+$/






share|improve this answer





















  • 1





    Careful, ^ and $ are beginning/ending of line in Ruby, not string. You almost always want A and z instead.

    – mu is too short
    Jan 4 at 19:30












Your Answer






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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54040199%2fregular-expression-for-letters-spaces-and-hyphens%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This regex will allow letters, spaces and hyphens: /^[A-Za-zs-]+$/






share|improve this answer





















  • 1





    Careful, ^ and $ are beginning/ending of line in Ruby, not string. You almost always want A and z instead.

    – mu is too short
    Jan 4 at 19:30
















0














This regex will allow letters, spaces and hyphens: /^[A-Za-zs-]+$/






share|improve this answer





















  • 1





    Careful, ^ and $ are beginning/ending of line in Ruby, not string. You almost always want A and z instead.

    – mu is too short
    Jan 4 at 19:30














0












0








0







This regex will allow letters, spaces and hyphens: /^[A-Za-zs-]+$/






share|improve this answer















This regex will allow letters, spaces and hyphens: /^[A-Za-zs-]+$/







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 16:34









Michael Pratt

2,4871126




2,4871126










answered Jan 4 at 16:02









MukeshMukesh

782719




782719








  • 1





    Careful, ^ and $ are beginning/ending of line in Ruby, not string. You almost always want A and z instead.

    – mu is too short
    Jan 4 at 19:30














  • 1





    Careful, ^ and $ are beginning/ending of line in Ruby, not string. You almost always want A and z instead.

    – mu is too short
    Jan 4 at 19:30








1




1





Careful, ^ and $ are beginning/ending of line in Ruby, not string. You almost always want A and z instead.

– mu is too short
Jan 4 at 19:30





Careful, ^ and $ are beginning/ending of line in Ruby, not string. You almost always want A and z instead.

– mu is too short
Jan 4 at 19:30




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54040199%2fregular-expression-for-letters-spaces-and-hyphens%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas