How to add dot separator on different positions of a number in Python?












1















I am trying to capture a number from a string, which sometimes contains dot separators and sometimes it does not. In any case I need a number with the dot separator.



e.g.:



num = re.findall('d{3}.(?:d{2}.){4}d{3}|d{14}', txt)[0]


will capture both variations:



304.33.44.52.03.002
30433445203002


In case it captured the one without dots, I would need to add the dots with the systematic of:



AAA.BB.CC.DD.EE.FFF


How can I add those dots with Python?










share|improve this question

























  • quick tip for your second question, consider the following bit of code: '{}{}{}.{}{}.{}{}.{}{}.{}{}.{}{}{}'.format(*list("30.4.33445203002".replace('.',''))) you can feed this your captures and they will always print in your format regardless of dot separation

    – Nullman
    Dec 30 '18 at 8:50











  • Why don't you do it using regex itself?

    – Pushpesh Kumar Rajwanshi
    Dec 30 '18 at 8:51











  • If this is possible with regex, sure would be an option. @Nullman This sounds like a great option. Could you craft an answer from this please?

    – merlin
    Dec 30 '18 at 8:54
















1















I am trying to capture a number from a string, which sometimes contains dot separators and sometimes it does not. In any case I need a number with the dot separator.



e.g.:



num = re.findall('d{3}.(?:d{2}.){4}d{3}|d{14}', txt)[0]


will capture both variations:



304.33.44.52.03.002
30433445203002


In case it captured the one without dots, I would need to add the dots with the systematic of:



AAA.BB.CC.DD.EE.FFF


How can I add those dots with Python?










share|improve this question

























  • quick tip for your second question, consider the following bit of code: '{}{}{}.{}{}.{}{}.{}{}.{}{}.{}{}{}'.format(*list("30.4.33445203002".replace('.',''))) you can feed this your captures and they will always print in your format regardless of dot separation

    – Nullman
    Dec 30 '18 at 8:50











  • Why don't you do it using regex itself?

    – Pushpesh Kumar Rajwanshi
    Dec 30 '18 at 8:51











  • If this is possible with regex, sure would be an option. @Nullman This sounds like a great option. Could you craft an answer from this please?

    – merlin
    Dec 30 '18 at 8:54














1












1








1








I am trying to capture a number from a string, which sometimes contains dot separators and sometimes it does not. In any case I need a number with the dot separator.



e.g.:



num = re.findall('d{3}.(?:d{2}.){4}d{3}|d{14}', txt)[0]


will capture both variations:



304.33.44.52.03.002
30433445203002


In case it captured the one without dots, I would need to add the dots with the systematic of:



AAA.BB.CC.DD.EE.FFF


How can I add those dots with Python?










share|improve this question
















I am trying to capture a number from a string, which sometimes contains dot separators and sometimes it does not. In any case I need a number with the dot separator.



e.g.:



num = re.findall('d{3}.(?:d{2}.){4}d{3}|d{14}', txt)[0]


will capture both variations:



304.33.44.52.03.002
30433445203002


In case it captured the one without dots, I would need to add the dots with the systematic of:



AAA.BB.CC.DD.EE.FFF


How can I add those dots with Python?







python regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 9:06









user2314737

14.6k115469




14.6k115469










asked Dec 30 '18 at 8:39









merlinmerlin

6901921




6901921













  • quick tip for your second question, consider the following bit of code: '{}{}{}.{}{}.{}{}.{}{}.{}{}.{}{}{}'.format(*list("30.4.33445203002".replace('.',''))) you can feed this your captures and they will always print in your format regardless of dot separation

    – Nullman
    Dec 30 '18 at 8:50











  • Why don't you do it using regex itself?

    – Pushpesh Kumar Rajwanshi
    Dec 30 '18 at 8:51











  • If this is possible with regex, sure would be an option. @Nullman This sounds like a great option. Could you craft an answer from this please?

    – merlin
    Dec 30 '18 at 8:54



















  • quick tip for your second question, consider the following bit of code: '{}{}{}.{}{}.{}{}.{}{}.{}{}.{}{}{}'.format(*list("30.4.33445203002".replace('.',''))) you can feed this your captures and they will always print in your format regardless of dot separation

    – Nullman
    Dec 30 '18 at 8:50











  • Why don't you do it using regex itself?

    – Pushpesh Kumar Rajwanshi
    Dec 30 '18 at 8:51











  • If this is possible with regex, sure would be an option. @Nullman This sounds like a great option. Could you craft an answer from this please?

    – merlin
    Dec 30 '18 at 8:54

















quick tip for your second question, consider the following bit of code: '{}{}{}.{}{}.{}{}.{}{}.{}{}.{}{}{}'.format(*list("30.4.33445203002".replace('.',''))) you can feed this your captures and they will always print in your format regardless of dot separation

– Nullman
Dec 30 '18 at 8:50





quick tip for your second question, consider the following bit of code: '{}{}{}.{}{}.{}{}.{}{}.{}{}.{}{}{}'.format(*list("30.4.33445203002".replace('.',''))) you can feed this your captures and they will always print in your format regardless of dot separation

– Nullman
Dec 30 '18 at 8:50













Why don't you do it using regex itself?

– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 8:51





Why don't you do it using regex itself?

– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 8:51













If this is possible with regex, sure would be an option. @Nullman This sounds like a great option. Could you craft an answer from this please?

– merlin
Dec 30 '18 at 8:54





If this is possible with regex, sure would be an option. @Nullman This sounds like a great option. Could you craft an answer from this please?

– merlin
Dec 30 '18 at 8:54












2 Answers
2






active

oldest

votes


















1














Solution without regexp.



You can transform it to list and insert dots in required positions, ensuring that value is string.



n = 30433445203002
l = list(str(n))


Add dots in positions you need



l.insert(3, '.')
l.insert(6, '.')
l.insert(9, '.')
l.insert(12, '.')
l.insert(15, '.')


If this is well-defined pattern. You can generalize the insertion above.



After insertion is done, join them back to the string:



num = "".join(l)


Input:



30433445203002


Output:



304.33.44.52.03.002





share|improve this answer

































    1














    You can capture each "group" of numbers into a capturing group, and refer to it in the replacement string. The dots can be made optional with .?.



    string = "30433445203002"
    regex = r"(d{3}).?(d{2}).?(d{2}).?(d{2}).?(d{2}).?(d{3})"
    pattern = "\1.\2.\3.\4.\5.\6"
    result = re.sub(regex, pattern, string)


    For more details, take a look on re.sub



    Output:



    304.33.44.52.03.002


    Regex Demo



    EDIT:



    If I have misunderstood you and what you actually want is to get the first 3 numbers, 4th and 5th numbers, 6th and 7th numbers etc, you can use the same regex with search:



    re.search(regex, string).group(1) # 304
    re.search(regex, string).group(2) # 33





    share|improve this answer


























    • What python do you use? I tried your solution and it seemed pretty interesting but in my interpreter I am getting "$1.$2.$3.$4.$5.$6" on your example.

      – Dmytro Chasovskyi
      Dec 30 '18 at 9:02











    • @DmytroChasovskyi Edited. I couldn't remember which syntax does python use. It's either $n or n. Now it should work.

      – Sweeper
      Dec 30 '18 at 9:04











    • your solution looks good and works for me. The only problem with pattern that it matches also other strings. For example, 304.33445203.002 and many more that potentially shouldn't be match. Do you know how to resolve it?

      – Dmytro Chasovskyi
      Dec 30 '18 at 9:15











    • @DmytroChasovskyi My solution only adds the dots. If you want to get the number in each group, you can use the regex with search and then call group. But if you want to not match anything that is not that pattern, you can use something like (d{3}).(d{2}).(d{2}).(d{2}).(d{2}).(d{3})|(d{3})(d{2})(d{2})(d{2})(d{2})(d{3}).

      – Sweeper
      Dec 30 '18 at 9:36











    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%2f53976250%2fhow-to-add-dot-separator-on-different-positions-of-a-number-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Solution without regexp.



    You can transform it to list and insert dots in required positions, ensuring that value is string.



    n = 30433445203002
    l = list(str(n))


    Add dots in positions you need



    l.insert(3, '.')
    l.insert(6, '.')
    l.insert(9, '.')
    l.insert(12, '.')
    l.insert(15, '.')


    If this is well-defined pattern. You can generalize the insertion above.



    After insertion is done, join them back to the string:



    num = "".join(l)


    Input:



    30433445203002


    Output:



    304.33.44.52.03.002





    share|improve this answer






























      1














      Solution without regexp.



      You can transform it to list and insert dots in required positions, ensuring that value is string.



      n = 30433445203002
      l = list(str(n))


      Add dots in positions you need



      l.insert(3, '.')
      l.insert(6, '.')
      l.insert(9, '.')
      l.insert(12, '.')
      l.insert(15, '.')


      If this is well-defined pattern. You can generalize the insertion above.



      After insertion is done, join them back to the string:



      num = "".join(l)


      Input:



      30433445203002


      Output:



      304.33.44.52.03.002





      share|improve this answer




























        1












        1








        1







        Solution without regexp.



        You can transform it to list and insert dots in required positions, ensuring that value is string.



        n = 30433445203002
        l = list(str(n))


        Add dots in positions you need



        l.insert(3, '.')
        l.insert(6, '.')
        l.insert(9, '.')
        l.insert(12, '.')
        l.insert(15, '.')


        If this is well-defined pattern. You can generalize the insertion above.



        After insertion is done, join them back to the string:



        num = "".join(l)


        Input:



        30433445203002


        Output:



        304.33.44.52.03.002





        share|improve this answer















        Solution without regexp.



        You can transform it to list and insert dots in required positions, ensuring that value is string.



        n = 30433445203002
        l = list(str(n))


        Add dots in positions you need



        l.insert(3, '.')
        l.insert(6, '.')
        l.insert(9, '.')
        l.insert(12, '.')
        l.insert(15, '.')


        If this is well-defined pattern. You can generalize the insertion above.



        After insertion is done, join them back to the string:



        num = "".join(l)


        Input:



        30433445203002


        Output:



        304.33.44.52.03.002






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 30 '18 at 9:05

























        answered Dec 30 '18 at 8:55









        Dmytro ChasovskyiDmytro Chasovskyi

        352320




        352320

























            1














            You can capture each "group" of numbers into a capturing group, and refer to it in the replacement string. The dots can be made optional with .?.



            string = "30433445203002"
            regex = r"(d{3}).?(d{2}).?(d{2}).?(d{2}).?(d{2}).?(d{3})"
            pattern = "\1.\2.\3.\4.\5.\6"
            result = re.sub(regex, pattern, string)


            For more details, take a look on re.sub



            Output:



            304.33.44.52.03.002


            Regex Demo



            EDIT:



            If I have misunderstood you and what you actually want is to get the first 3 numbers, 4th and 5th numbers, 6th and 7th numbers etc, you can use the same regex with search:



            re.search(regex, string).group(1) # 304
            re.search(regex, string).group(2) # 33





            share|improve this answer


























            • What python do you use? I tried your solution and it seemed pretty interesting but in my interpreter I am getting "$1.$2.$3.$4.$5.$6" on your example.

              – Dmytro Chasovskyi
              Dec 30 '18 at 9:02











            • @DmytroChasovskyi Edited. I couldn't remember which syntax does python use. It's either $n or n. Now it should work.

              – Sweeper
              Dec 30 '18 at 9:04











            • your solution looks good and works for me. The only problem with pattern that it matches also other strings. For example, 304.33445203.002 and many more that potentially shouldn't be match. Do you know how to resolve it?

              – Dmytro Chasovskyi
              Dec 30 '18 at 9:15











            • @DmytroChasovskyi My solution only adds the dots. If you want to get the number in each group, you can use the regex with search and then call group. But if you want to not match anything that is not that pattern, you can use something like (d{3}).(d{2}).(d{2}).(d{2}).(d{2}).(d{3})|(d{3})(d{2})(d{2})(d{2})(d{2})(d{3}).

              – Sweeper
              Dec 30 '18 at 9:36
















            1














            You can capture each "group" of numbers into a capturing group, and refer to it in the replacement string. The dots can be made optional with .?.



            string = "30433445203002"
            regex = r"(d{3}).?(d{2}).?(d{2}).?(d{2}).?(d{2}).?(d{3})"
            pattern = "\1.\2.\3.\4.\5.\6"
            result = re.sub(regex, pattern, string)


            For more details, take a look on re.sub



            Output:



            304.33.44.52.03.002


            Regex Demo



            EDIT:



            If I have misunderstood you and what you actually want is to get the first 3 numbers, 4th and 5th numbers, 6th and 7th numbers etc, you can use the same regex with search:



            re.search(regex, string).group(1) # 304
            re.search(regex, string).group(2) # 33





            share|improve this answer


























            • What python do you use? I tried your solution and it seemed pretty interesting but in my interpreter I am getting "$1.$2.$3.$4.$5.$6" on your example.

              – Dmytro Chasovskyi
              Dec 30 '18 at 9:02











            • @DmytroChasovskyi Edited. I couldn't remember which syntax does python use. It's either $n or n. Now it should work.

              – Sweeper
              Dec 30 '18 at 9:04











            • your solution looks good and works for me. The only problem with pattern that it matches also other strings. For example, 304.33445203.002 and many more that potentially shouldn't be match. Do you know how to resolve it?

              – Dmytro Chasovskyi
              Dec 30 '18 at 9:15











            • @DmytroChasovskyi My solution only adds the dots. If you want to get the number in each group, you can use the regex with search and then call group. But if you want to not match anything that is not that pattern, you can use something like (d{3}).(d{2}).(d{2}).(d{2}).(d{2}).(d{3})|(d{3})(d{2})(d{2})(d{2})(d{2})(d{3}).

              – Sweeper
              Dec 30 '18 at 9:36














            1












            1








            1







            You can capture each "group" of numbers into a capturing group, and refer to it in the replacement string. The dots can be made optional with .?.



            string = "30433445203002"
            regex = r"(d{3}).?(d{2}).?(d{2}).?(d{2}).?(d{2}).?(d{3})"
            pattern = "\1.\2.\3.\4.\5.\6"
            result = re.sub(regex, pattern, string)


            For more details, take a look on re.sub



            Output:



            304.33.44.52.03.002


            Regex Demo



            EDIT:



            If I have misunderstood you and what you actually want is to get the first 3 numbers, 4th and 5th numbers, 6th and 7th numbers etc, you can use the same regex with search:



            re.search(regex, string).group(1) # 304
            re.search(regex, string).group(2) # 33





            share|improve this answer















            You can capture each "group" of numbers into a capturing group, and refer to it in the replacement string. The dots can be made optional with .?.



            string = "30433445203002"
            regex = r"(d{3}).?(d{2}).?(d{2}).?(d{2}).?(d{2}).?(d{3})"
            pattern = "\1.\2.\3.\4.\5.\6"
            result = re.sub(regex, pattern, string)


            For more details, take a look on re.sub



            Output:



            304.33.44.52.03.002


            Regex Demo



            EDIT:



            If I have misunderstood you and what you actually want is to get the first 3 numbers, 4th and 5th numbers, 6th and 7th numbers etc, you can use the same regex with search:



            re.search(regex, string).group(1) # 304
            re.search(regex, string).group(2) # 33






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 30 '18 at 9:41

























            answered Dec 30 '18 at 8:53









            SweeperSweeper

            66.2k1073139




            66.2k1073139













            • What python do you use? I tried your solution and it seemed pretty interesting but in my interpreter I am getting "$1.$2.$3.$4.$5.$6" on your example.

              – Dmytro Chasovskyi
              Dec 30 '18 at 9:02











            • @DmytroChasovskyi Edited. I couldn't remember which syntax does python use. It's either $n or n. Now it should work.

              – Sweeper
              Dec 30 '18 at 9:04











            • your solution looks good and works for me. The only problem with pattern that it matches also other strings. For example, 304.33445203.002 and many more that potentially shouldn't be match. Do you know how to resolve it?

              – Dmytro Chasovskyi
              Dec 30 '18 at 9:15











            • @DmytroChasovskyi My solution only adds the dots. If you want to get the number in each group, you can use the regex with search and then call group. But if you want to not match anything that is not that pattern, you can use something like (d{3}).(d{2}).(d{2}).(d{2}).(d{2}).(d{3})|(d{3})(d{2})(d{2})(d{2})(d{2})(d{3}).

              – Sweeper
              Dec 30 '18 at 9:36



















            • What python do you use? I tried your solution and it seemed pretty interesting but in my interpreter I am getting "$1.$2.$3.$4.$5.$6" on your example.

              – Dmytro Chasovskyi
              Dec 30 '18 at 9:02











            • @DmytroChasovskyi Edited. I couldn't remember which syntax does python use. It's either $n or n. Now it should work.

              – Sweeper
              Dec 30 '18 at 9:04











            • your solution looks good and works for me. The only problem with pattern that it matches also other strings. For example, 304.33445203.002 and many more that potentially shouldn't be match. Do you know how to resolve it?

              – Dmytro Chasovskyi
              Dec 30 '18 at 9:15











            • @DmytroChasovskyi My solution only adds the dots. If you want to get the number in each group, you can use the regex with search and then call group. But if you want to not match anything that is not that pattern, you can use something like (d{3}).(d{2}).(d{2}).(d{2}).(d{2}).(d{3})|(d{3})(d{2})(d{2})(d{2})(d{2})(d{3}).

              – Sweeper
              Dec 30 '18 at 9:36

















            What python do you use? I tried your solution and it seemed pretty interesting but in my interpreter I am getting "$1.$2.$3.$4.$5.$6" on your example.

            – Dmytro Chasovskyi
            Dec 30 '18 at 9:02





            What python do you use? I tried your solution and it seemed pretty interesting but in my interpreter I am getting "$1.$2.$3.$4.$5.$6" on your example.

            – Dmytro Chasovskyi
            Dec 30 '18 at 9:02













            @DmytroChasovskyi Edited. I couldn't remember which syntax does python use. It's either $n or n. Now it should work.

            – Sweeper
            Dec 30 '18 at 9:04





            @DmytroChasovskyi Edited. I couldn't remember which syntax does python use. It's either $n or n. Now it should work.

            – Sweeper
            Dec 30 '18 at 9:04













            your solution looks good and works for me. The only problem with pattern that it matches also other strings. For example, 304.33445203.002 and many more that potentially shouldn't be match. Do you know how to resolve it?

            – Dmytro Chasovskyi
            Dec 30 '18 at 9:15





            your solution looks good and works for me. The only problem with pattern that it matches also other strings. For example, 304.33445203.002 and many more that potentially shouldn't be match. Do you know how to resolve it?

            – Dmytro Chasovskyi
            Dec 30 '18 at 9:15













            @DmytroChasovskyi My solution only adds the dots. If you want to get the number in each group, you can use the regex with search and then call group. But if you want to not match anything that is not that pattern, you can use something like (d{3}).(d{2}).(d{2}).(d{2}).(d{2}).(d{3})|(d{3})(d{2})(d{2})(d{2})(d{2})(d{3}).

            – Sweeper
            Dec 30 '18 at 9:36





            @DmytroChasovskyi My solution only adds the dots. If you want to get the number in each group, you can use the regex with search and then call group. But if you want to not match anything that is not that pattern, you can use something like (d{3}).(d{2}).(d{2}).(d{2}).(d{2}).(d{3})|(d{3})(d{2})(d{2})(d{2})(d{2})(d{3}).

            – Sweeper
            Dec 30 '18 at 9:36


















            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%2f53976250%2fhow-to-add-dot-separator-on-different-positions-of-a-number-in-python%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

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'