regex for allowing SINGLE white space in between words












1















I m using the following regex for the same:
^S.*S$



but this regex is not working in case of multiple spaces in between words.
and one more thing, it doesnt allow you to enter a single character



I want to validate no leading and trailing space and single white space in between words, words can include anything alphanumeric as well as special character.



if i m trying to enter 'a' in the text box, its not accepting










share|improve this question

























  • "Working" and "not working" suggest you know what you want, but I can't tell from reading this what that is. Provide some examples of input and desired output and you'll get better answers.

    – Edward
    Dec 5 '14 at 11:59











  • What are you trying to achieve? Post some examples for valid and invalid matches.

    – Avinash Raj
    Dec 5 '14 at 11:59











  • this regex should give true for following: shashank shekhar

    – Shashank Shekhar
    Dec 5 '14 at 12:00






  • 1





    It will be better if your provide examples here. For input A expected output will be B.

    – fhnaseer
    Dec 5 '14 at 12:01






  • 1





    Add example in your question, not in comments,

    – fhnaseer
    Dec 5 '14 at 12:04
















1















I m using the following regex for the same:
^S.*S$



but this regex is not working in case of multiple spaces in between words.
and one more thing, it doesnt allow you to enter a single character



I want to validate no leading and trailing space and single white space in between words, words can include anything alphanumeric as well as special character.



if i m trying to enter 'a' in the text box, its not accepting










share|improve this question

























  • "Working" and "not working" suggest you know what you want, but I can't tell from reading this what that is. Provide some examples of input and desired output and you'll get better answers.

    – Edward
    Dec 5 '14 at 11:59











  • What are you trying to achieve? Post some examples for valid and invalid matches.

    – Avinash Raj
    Dec 5 '14 at 11:59











  • this regex should give true for following: shashank shekhar

    – Shashank Shekhar
    Dec 5 '14 at 12:00






  • 1





    It will be better if your provide examples here. For input A expected output will be B.

    – fhnaseer
    Dec 5 '14 at 12:01






  • 1





    Add example in your question, not in comments,

    – fhnaseer
    Dec 5 '14 at 12:04














1












1








1








I m using the following regex for the same:
^S.*S$



but this regex is not working in case of multiple spaces in between words.
and one more thing, it doesnt allow you to enter a single character



I want to validate no leading and trailing space and single white space in between words, words can include anything alphanumeric as well as special character.



if i m trying to enter 'a' in the text box, its not accepting










share|improve this question
















I m using the following regex for the same:
^S.*S$



but this regex is not working in case of multiple spaces in between words.
and one more thing, it doesnt allow you to enter a single character



I want to validate no leading and trailing space and single white space in between words, words can include anything alphanumeric as well as special character.



if i m trying to enter 'a' in the text box, its not accepting







regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 '14 at 12:13







Shashank Shekhar

















asked Dec 5 '14 at 11:57









Shashank ShekharShashank Shekhar

1315




1315













  • "Working" and "not working" suggest you know what you want, but I can't tell from reading this what that is. Provide some examples of input and desired output and you'll get better answers.

    – Edward
    Dec 5 '14 at 11:59











  • What are you trying to achieve? Post some examples for valid and invalid matches.

    – Avinash Raj
    Dec 5 '14 at 11:59











  • this regex should give true for following: shashank shekhar

    – Shashank Shekhar
    Dec 5 '14 at 12:00






  • 1





    It will be better if your provide examples here. For input A expected output will be B.

    – fhnaseer
    Dec 5 '14 at 12:01






  • 1





    Add example in your question, not in comments,

    – fhnaseer
    Dec 5 '14 at 12:04



















  • "Working" and "not working" suggest you know what you want, but I can't tell from reading this what that is. Provide some examples of input and desired output and you'll get better answers.

    – Edward
    Dec 5 '14 at 11:59











  • What are you trying to achieve? Post some examples for valid and invalid matches.

    – Avinash Raj
    Dec 5 '14 at 11:59











  • this regex should give true for following: shashank shekhar

    – Shashank Shekhar
    Dec 5 '14 at 12:00






  • 1





    It will be better if your provide examples here. For input A expected output will be B.

    – fhnaseer
    Dec 5 '14 at 12:01






  • 1





    Add example in your question, not in comments,

    – fhnaseer
    Dec 5 '14 at 12:04

















"Working" and "not working" suggest you know what you want, but I can't tell from reading this what that is. Provide some examples of input and desired output and you'll get better answers.

– Edward
Dec 5 '14 at 11:59





"Working" and "not working" suggest you know what you want, but I can't tell from reading this what that is. Provide some examples of input and desired output and you'll get better answers.

– Edward
Dec 5 '14 at 11:59













What are you trying to achieve? Post some examples for valid and invalid matches.

– Avinash Raj
Dec 5 '14 at 11:59





What are you trying to achieve? Post some examples for valid and invalid matches.

– Avinash Raj
Dec 5 '14 at 11:59













this regex should give true for following: shashank shekhar

– Shashank Shekhar
Dec 5 '14 at 12:00





this regex should give true for following: shashank shekhar

– Shashank Shekhar
Dec 5 '14 at 12:00




1




1





It will be better if your provide examples here. For input A expected output will be B.

– fhnaseer
Dec 5 '14 at 12:01





It will be better if your provide examples here. For input A expected output will be B.

– fhnaseer
Dec 5 '14 at 12:01




1




1





Add example in your question, not in comments,

– fhnaseer
Dec 5 '14 at 12:04





Add example in your question, not in comments,

– fhnaseer
Dec 5 '14 at 12:04












3 Answers
3






active

oldest

votes


















1














Seems like you want something like this,



^S+(?: S+)*$


S matches any non-space character.



DEMO






share|improve this answer



















  • 1





    thanks buddy..this worked :)

    – Shashank Shekhar
    Dec 5 '14 at 12:25



















0














[A-Za-z]+(s[A-Za-z]+)? you can use this to put single space between words






share|improve this answer
























  • i need to use alphanumeric as well as special characters and its not working if i want to enter a single character

    – Shashank Shekhar
    Dec 5 '14 at 12:05













  • @ShashankShekhar why you failed to add explanation in your question?

    – Avinash Raj
    Dec 5 '14 at 12:09



















0














Just assert that two spaces don't appear;



^(?!.*ss)S(.*S)?$





share|improve this answer























    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%2f27315412%2fregex-for-allowing-single-white-space-in-between-words%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Seems like you want something like this,



    ^S+(?: S+)*$


    S matches any non-space character.



    DEMO






    share|improve this answer



















    • 1





      thanks buddy..this worked :)

      – Shashank Shekhar
      Dec 5 '14 at 12:25
















    1














    Seems like you want something like this,



    ^S+(?: S+)*$


    S matches any non-space character.



    DEMO






    share|improve this answer



















    • 1





      thanks buddy..this worked :)

      – Shashank Shekhar
      Dec 5 '14 at 12:25














    1












    1








    1







    Seems like you want something like this,



    ^S+(?: S+)*$


    S matches any non-space character.



    DEMO






    share|improve this answer













    Seems like you want something like this,



    ^S+(?: S+)*$


    S matches any non-space character.



    DEMO







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 5 '14 at 12:20









    Avinash RajAvinash Raj

    142k14115162




    142k14115162








    • 1





      thanks buddy..this worked :)

      – Shashank Shekhar
      Dec 5 '14 at 12:25














    • 1





      thanks buddy..this worked :)

      – Shashank Shekhar
      Dec 5 '14 at 12:25








    1




    1





    thanks buddy..this worked :)

    – Shashank Shekhar
    Dec 5 '14 at 12:25





    thanks buddy..this worked :)

    – Shashank Shekhar
    Dec 5 '14 at 12:25













    0














    [A-Za-z]+(s[A-Za-z]+)? you can use this to put single space between words






    share|improve this answer
























    • i need to use alphanumeric as well as special characters and its not working if i want to enter a single character

      – Shashank Shekhar
      Dec 5 '14 at 12:05













    • @ShashankShekhar why you failed to add explanation in your question?

      – Avinash Raj
      Dec 5 '14 at 12:09
















    0














    [A-Za-z]+(s[A-Za-z]+)? you can use this to put single space between words






    share|improve this answer
























    • i need to use alphanumeric as well as special characters and its not working if i want to enter a single character

      – Shashank Shekhar
      Dec 5 '14 at 12:05













    • @ShashankShekhar why you failed to add explanation in your question?

      – Avinash Raj
      Dec 5 '14 at 12:09














    0












    0








    0







    [A-Za-z]+(s[A-Za-z]+)? you can use this to put single space between words






    share|improve this answer













    [A-Za-z]+(s[A-Za-z]+)? you can use this to put single space between words







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 5 '14 at 12:01









    Sandy RawatSandy Rawat

    399410




    399410













    • i need to use alphanumeric as well as special characters and its not working if i want to enter a single character

      – Shashank Shekhar
      Dec 5 '14 at 12:05













    • @ShashankShekhar why you failed to add explanation in your question?

      – Avinash Raj
      Dec 5 '14 at 12:09



















    • i need to use alphanumeric as well as special characters and its not working if i want to enter a single character

      – Shashank Shekhar
      Dec 5 '14 at 12:05













    • @ShashankShekhar why you failed to add explanation in your question?

      – Avinash Raj
      Dec 5 '14 at 12:09

















    i need to use alphanumeric as well as special characters and its not working if i want to enter a single character

    – Shashank Shekhar
    Dec 5 '14 at 12:05







    i need to use alphanumeric as well as special characters and its not working if i want to enter a single character

    – Shashank Shekhar
    Dec 5 '14 at 12:05















    @ShashankShekhar why you failed to add explanation in your question?

    – Avinash Raj
    Dec 5 '14 at 12:09





    @ShashankShekhar why you failed to add explanation in your question?

    – Avinash Raj
    Dec 5 '14 at 12:09











    0














    Just assert that two spaces don't appear;



    ^(?!.*ss)S(.*S)?$





    share|improve this answer




























      0














      Just assert that two spaces don't appear;



      ^(?!.*ss)S(.*S)?$





      share|improve this answer


























        0












        0








        0







        Just assert that two spaces don't appear;



        ^(?!.*ss)S(.*S)?$





        share|improve this answer













        Just assert that two spaces don't appear;



        ^(?!.*ss)S(.*S)?$






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 5 '14 at 12:26









        BohemianBohemian

        295k64415553




        295k64415553






























            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%2f27315412%2fregex-for-allowing-single-white-space-in-between-words%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

            Bressuire

            Vorschmack

            Quarantine