regular express issue with 1 character string









up vote
-1
down vote

favorite












I am allowing only alpha-numeric, _ & - values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet or numeric or _ or -), I got empty value instead of single charter.



Here is sample code



$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);


or



$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);


I have tested this multiple versions of PHP as well










share|improve this question



















  • 1




    You might want to remove ^ and $ and use '/[^a-zA-Z0-9_-]/' (a negated character class) if you want to remove those chars you do not want anywhere in a string.
    – Wiktor Stribiżew
    Nov 9 at 20:35











  • After removing ^ & $ result is same
    – Hassaan
    Nov 9 at 20:37










  • I meant preg_replace('/[^a-zA-Z0-9_-]/', '', $str), => string(1) "a"
    – Wiktor Stribiżew
    Nov 9 at 20:38











  • What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use preg_match instead
    – user3783243
    Nov 9 at 20:39







  • 1




    If that works, you may also use preg_replace('/[^w-]+/', '', $str)
    – Wiktor Stribiżew
    Nov 9 at 20:41














up vote
-1
down vote

favorite












I am allowing only alpha-numeric, _ & - values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet or numeric or _ or -), I got empty value instead of single charter.



Here is sample code



$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);


or



$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);


I have tested this multiple versions of PHP as well










share|improve this question



















  • 1




    You might want to remove ^ and $ and use '/[^a-zA-Z0-9_-]/' (a negated character class) if you want to remove those chars you do not want anywhere in a string.
    – Wiktor Stribiżew
    Nov 9 at 20:35











  • After removing ^ & $ result is same
    – Hassaan
    Nov 9 at 20:37










  • I meant preg_replace('/[^a-zA-Z0-9_-]/', '', $str), => string(1) "a"
    – Wiktor Stribiżew
    Nov 9 at 20:38











  • What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use preg_match instead
    – user3783243
    Nov 9 at 20:39







  • 1




    If that works, you may also use preg_replace('/[^w-]+/', '', $str)
    – Wiktor Stribiżew
    Nov 9 at 20:41












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am allowing only alpha-numeric, _ & - values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet or numeric or _ or -), I got empty value instead of single charter.



Here is sample code



$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);


or



$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);


I have tested this multiple versions of PHP as well










share|improve this question















I am allowing only alpha-numeric, _ & - values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet or numeric or _ or -), I got empty value instead of single charter.



Here is sample code



$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);


or



$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);


I have tested this multiple versions of PHP as well







php regex preg-replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 12:53

























asked Nov 9 at 20:34









Hassaan

4,91541337




4,91541337







  • 1




    You might want to remove ^ and $ and use '/[^a-zA-Z0-9_-]/' (a negated character class) if you want to remove those chars you do not want anywhere in a string.
    – Wiktor Stribiżew
    Nov 9 at 20:35











  • After removing ^ & $ result is same
    – Hassaan
    Nov 9 at 20:37










  • I meant preg_replace('/[^a-zA-Z0-9_-]/', '', $str), => string(1) "a"
    – Wiktor Stribiżew
    Nov 9 at 20:38











  • What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use preg_match instead
    – user3783243
    Nov 9 at 20:39







  • 1




    If that works, you may also use preg_replace('/[^w-]+/', '', $str)
    – Wiktor Stribiżew
    Nov 9 at 20:41












  • 1




    You might want to remove ^ and $ and use '/[^a-zA-Z0-9_-]/' (a negated character class) if you want to remove those chars you do not want anywhere in a string.
    – Wiktor Stribiżew
    Nov 9 at 20:35











  • After removing ^ & $ result is same
    – Hassaan
    Nov 9 at 20:37










  • I meant preg_replace('/[^a-zA-Z0-9_-]/', '', $str), => string(1) "a"
    – Wiktor Stribiżew
    Nov 9 at 20:38











  • What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use preg_match instead
    – user3783243
    Nov 9 at 20:39







  • 1




    If that works, you may also use preg_replace('/[^w-]+/', '', $str)
    – Wiktor Stribiżew
    Nov 9 at 20:41







1




1




You might want to remove ^ and $ and use '/[^a-zA-Z0-9_-]/' (a negated character class) if you want to remove those chars you do not want anywhere in a string.
– Wiktor Stribiżew
Nov 9 at 20:35





You might want to remove ^ and $ and use '/[^a-zA-Z0-9_-]/' (a negated character class) if you want to remove those chars you do not want anywhere in a string.
– Wiktor Stribiżew
Nov 9 at 20:35













After removing ^ & $ result is same
– Hassaan
Nov 9 at 20:37




After removing ^ & $ result is same
– Hassaan
Nov 9 at 20:37












I meant preg_replace('/[^a-zA-Z0-9_-]/', '', $str), => string(1) "a"
– Wiktor Stribiżew
Nov 9 at 20:38





I meant preg_replace('/[^a-zA-Z0-9_-]/', '', $str), => string(1) "a"
– Wiktor Stribiżew
Nov 9 at 20:38













What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use preg_match instead
– user3783243
Nov 9 at 20:39





What is expected? You are replacing alpha numerical characters with nothing so an empty string is what I'd expect. Maybe use preg_match instead
– user3783243
Nov 9 at 20:39





1




1




If that works, you may also use preg_replace('/[^w-]+/', '', $str)
– Wiktor Stribiżew
Nov 9 at 20:41




If that works, you may also use preg_replace('/[^w-]+/', '', $str)
– Wiktor Stribiżew
Nov 9 at 20:41












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You are removing any chars other than ASCII letters, digits, _ and - anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:



$str = preg_replace('/[^w-]+/', '', $str);


See the PHP demo online and a regex demo.



Details




  • [^ - start of a negated character class


    • w - a word char: letter, digit or _


    • - - a hyphen



  • ] - end of the character class


  • + - a quantifier: 1 or more repetitions.





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',
    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%2f53232920%2fregular-express-issue-with-1-character-string%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








    up vote
    1
    down vote



    accepted










    You are removing any chars other than ASCII letters, digits, _ and - anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:



    $str = preg_replace('/[^w-]+/', '', $str);


    See the PHP demo online and a regex demo.



    Details




    • [^ - start of a negated character class


      • w - a word char: letter, digit or _


      • - - a hyphen



    • ] - end of the character class


    • + - a quantifier: 1 or more repetitions.





    share|improve this answer
























      up vote
      1
      down vote



      accepted










      You are removing any chars other than ASCII letters, digits, _ and - anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:



      $str = preg_replace('/[^w-]+/', '', $str);


      See the PHP demo online and a regex demo.



      Details




      • [^ - start of a negated character class


        • w - a word char: letter, digit or _


        • - - a hyphen



      • ] - end of the character class


      • + - a quantifier: 1 or more repetitions.





      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You are removing any chars other than ASCII letters, digits, _ and - anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:



        $str = preg_replace('/[^w-]+/', '', $str);


        See the PHP demo online and a regex demo.



        Details




        • [^ - start of a negated character class


          • w - a word char: letter, digit or _


          • - - a hyphen



        • ] - end of the character class


        • + - a quantifier: 1 or more repetitions.





        share|improve this answer












        You are removing any chars other than ASCII letters, digits, _ and - anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:



        $str = preg_replace('/[^w-]+/', '', $str);


        See the PHP demo online and a regex demo.



        Details




        • [^ - start of a negated character class


          • w - a word char: letter, digit or _


          • - - a hyphen



        • ] - end of the character class


        • + - a quantifier: 1 or more repetitions.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 20:46









        Wiktor Stribiżew

        301k16122197




        301k16122197



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232920%2fregular-express-issue-with-1-character-string%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo