Extracting Numeric part with thousands separator and decimal point using regex in c# in a alpha numeric...












0















I am trying to extract only the number part including the thousands separator and the decimal value , but always getting the error input string not in correct format .



My input string will be in the following formats




USD1200 1,200.12 USD




I need the output as below respectively




1200 1,200.12




If I use the below the first occurrence of , is considered as delimiter



string prodValue = Regex.Match(prodAmount, @"d+").Value;


If I use this one , I am getting error



string prodValue = Regex.Match(prodAmount, @"^[0-9]+(,[0-9])+(.[0-9])?$").Value;


How can I can extract the output I needed?










share|improve this question

























  • So you just need to RegEx replace anything that isn't 0-9, "," or "." ?

    – Nathan Champion
    Nov 24 '18 at 6:40











  • Try Regex.Replace(s, @"[^0-9.,]+", "").Trim()

    – Wiktor Stribiżew
    Nov 24 '18 at 12:02
















0















I am trying to extract only the number part including the thousands separator and the decimal value , but always getting the error input string not in correct format .



My input string will be in the following formats




USD1200 1,200.12 USD




I need the output as below respectively




1200 1,200.12




If I use the below the first occurrence of , is considered as delimiter



string prodValue = Regex.Match(prodAmount, @"d+").Value;


If I use this one , I am getting error



string prodValue = Regex.Match(prodAmount, @"^[0-9]+(,[0-9])+(.[0-9])?$").Value;


How can I can extract the output I needed?










share|improve this question

























  • So you just need to RegEx replace anything that isn't 0-9, "," or "." ?

    – Nathan Champion
    Nov 24 '18 at 6:40











  • Try Regex.Replace(s, @"[^0-9.,]+", "").Trim()

    – Wiktor Stribiżew
    Nov 24 '18 at 12:02














0












0








0








I am trying to extract only the number part including the thousands separator and the decimal value , but always getting the error input string not in correct format .



My input string will be in the following formats




USD1200 1,200.12 USD




I need the output as below respectively




1200 1,200.12




If I use the below the first occurrence of , is considered as delimiter



string prodValue = Regex.Match(prodAmount, @"d+").Value;


If I use this one , I am getting error



string prodValue = Regex.Match(prodAmount, @"^[0-9]+(,[0-9])+(.[0-9])?$").Value;


How can I can extract the output I needed?










share|improve this question
















I am trying to extract only the number part including the thousands separator and the decimal value , but always getting the error input string not in correct format .



My input string will be in the following formats




USD1200 1,200.12 USD




I need the output as below respectively




1200 1,200.12




If I use the below the first occurrence of , is considered as delimiter



string prodValue = Regex.Match(prodAmount, @"d+").Value;


If I use this one , I am getting error



string prodValue = Regex.Match(prodAmount, @"^[0-9]+(,[0-9])+(.[0-9])?$").Value;


How can I can extract the output I needed?







c# regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 7:37









Maksym Labutin

367313




367313










asked Nov 24 '18 at 6:37









pragathi npragathi n

1




1













  • So you just need to RegEx replace anything that isn't 0-9, "," or "." ?

    – Nathan Champion
    Nov 24 '18 at 6:40











  • Try Regex.Replace(s, @"[^0-9.,]+", "").Trim()

    – Wiktor Stribiżew
    Nov 24 '18 at 12:02



















  • So you just need to RegEx replace anything that isn't 0-9, "," or "." ?

    – Nathan Champion
    Nov 24 '18 at 6:40











  • Try Regex.Replace(s, @"[^0-9.,]+", "").Trim()

    – Wiktor Stribiżew
    Nov 24 '18 at 12:02

















So you just need to RegEx replace anything that isn't 0-9, "," or "." ?

– Nathan Champion
Nov 24 '18 at 6:40





So you just need to RegEx replace anything that isn't 0-9, "," or "." ?

– Nathan Champion
Nov 24 '18 at 6:40













Try Regex.Replace(s, @"[^0-9.,]+", "").Trim()

– Wiktor Stribiżew
Nov 24 '18 at 12:02





Try Regex.Replace(s, @"[^0-9.,]+", "").Trim()

– Wiktor Stribiżew
Nov 24 '18 at 12:02












4 Answers
4






active

oldest

votes


















2














For capturing multiple matches, you need Regex.Matches instead of Regex.Match . Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.



You can use this regex,



d+(?:,d+)*(?:.d+)?


Explanation:





  • d+ --> Matches one or more digits


  • (?:,d+)* --> Matches a comma followed by one or more digits zero or more times


  • (?:.d+)? --> Optionally matches a literal dot followed by one or more digits


Demo



Here is a sample C# code,



MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
for (int i = 0; i < matches.Count;i++) {
Console.WriteLine("Match: " + matches[i]);
}


This gives following output like you expect,



Match: 1200
Match: 1,200.12
Match: 1,211,234,332.12
Match: 12,333





share|improve this answer

































    0














    I think you places the second "+" wrong and missed a third one.



    @"^[0-9]+,[0-9]+.[0-9]+?$")





    share|improve this answer































      0














      You Regex match string is incorrect.



      Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$



      See example here






      share|improve this answer


























      • You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.

        – Pushpesh Kumar Rajwanshi
        Nov 24 '18 at 9:04











      • Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.

        – Julo
        Nov 24 '18 at 9:15



















      0














      In your regex you use anchors ^ to assert the start and the end $ of the string where USD is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.



      To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*. Note that you don't have to escape the comma.



      If the USD is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.



      You could use named capturing group to reference the group by name.



      bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb



      That would match





      • bUSD Match word boundary and USD


      • (?<n1>d+) Named capturing group n1 which will match 1+ digits


      • | or


      • (?<n2> Start named capturing group n2



        • d{1,3} Match 1-3 digits


        • (?:,d{3})* Match a comma, 3 digits and repeat 0+ times


        • (?:.d+)? Match a dot and 1+ digits and make that optional




      • ) Close namedd capturing group


      • USDb Match space, USD and a word boundary


      Regex demo | C# demo






      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%2f53455799%2fextracting-numeric-part-with-thousands-separator-and-decimal-point-using-regex-i%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        For capturing multiple matches, you need Regex.Matches instead of Regex.Match . Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.



        You can use this regex,



        d+(?:,d+)*(?:.d+)?


        Explanation:





        • d+ --> Matches one or more digits


        • (?:,d+)* --> Matches a comma followed by one or more digits zero or more times


        • (?:.d+)? --> Optionally matches a literal dot followed by one or more digits


        Demo



        Here is a sample C# code,



        MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
        for (int i = 0; i < matches.Count;i++) {
        Console.WriteLine("Match: " + matches[i]);
        }


        This gives following output like you expect,



        Match: 1200
        Match: 1,200.12
        Match: 1,211,234,332.12
        Match: 12,333





        share|improve this answer






























          2














          For capturing multiple matches, you need Regex.Matches instead of Regex.Match . Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.



          You can use this regex,



          d+(?:,d+)*(?:.d+)?


          Explanation:





          • d+ --> Matches one or more digits


          • (?:,d+)* --> Matches a comma followed by one or more digits zero or more times


          • (?:.d+)? --> Optionally matches a literal dot followed by one or more digits


          Demo



          Here is a sample C# code,



          MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
          for (int i = 0; i < matches.Count;i++) {
          Console.WriteLine("Match: " + matches[i]);
          }


          This gives following output like you expect,



          Match: 1200
          Match: 1,200.12
          Match: 1,211,234,332.12
          Match: 12,333





          share|improve this answer




























            2












            2








            2







            For capturing multiple matches, you need Regex.Matches instead of Regex.Match . Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.



            You can use this regex,



            d+(?:,d+)*(?:.d+)?


            Explanation:





            • d+ --> Matches one or more digits


            • (?:,d+)* --> Matches a comma followed by one or more digits zero or more times


            • (?:.d+)? --> Optionally matches a literal dot followed by one or more digits


            Demo



            Here is a sample C# code,



            MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
            for (int i = 0; i < matches.Count;i++) {
            Console.WriteLine("Match: " + matches[i]);
            }


            This gives following output like you expect,



            Match: 1200
            Match: 1,200.12
            Match: 1,211,234,332.12
            Match: 12,333





            share|improve this answer















            For capturing multiple matches, you need Regex.Matches instead of Regex.Match . Also in your regex, you need to make comma group as zero or more occurence and dot group as optional as it may or may not exist in the number you intend to capture.



            You can use this regex,



            d+(?:,d+)*(?:.d+)?


            Explanation:





            • d+ --> Matches one or more digits


            • (?:,d+)* --> Matches a comma followed by one or more digits zero or more times


            • (?:.d+)? --> Optionally matches a literal dot followed by one or more digits


            Demo



            Here is a sample C# code,



            MatchCollection matches = Regex.Matches("USD1200 1,200.12 1,211,234,332.12 12,333 USD", @"d+(?:,d+)*(?:.d+)?");
            for (int i = 0; i < matches.Count;i++) {
            Console.WriteLine("Match: " + matches[i]);
            }


            This gives following output like you expect,



            Match: 1200
            Match: 1,200.12
            Match: 1,211,234,332.12
            Match: 12,333






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 8:49

























            answered Nov 24 '18 at 8:43









            Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

            7,6202927




            7,6202927

























                0














                I think you places the second "+" wrong and missed a third one.



                @"^[0-9]+,[0-9]+.[0-9]+?$")





                share|improve this answer




























                  0














                  I think you places the second "+" wrong and missed a third one.



                  @"^[0-9]+,[0-9]+.[0-9]+?$")





                  share|improve this answer


























                    0












                    0








                    0







                    I think you places the second "+" wrong and missed a third one.



                    @"^[0-9]+,[0-9]+.[0-9]+?$")





                    share|improve this answer













                    I think you places the second "+" wrong and missed a third one.



                    @"^[0-9]+,[0-9]+.[0-9]+?$")






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 6:42









                    Klaus GütterKlaus Gütter

                    2,50821321




                    2,50821321























                        0














                        You Regex match string is incorrect.



                        Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$



                        See example here






                        share|improve this answer


























                        • You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.

                          – Pushpesh Kumar Rajwanshi
                          Nov 24 '18 at 9:04











                        • Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.

                          – Julo
                          Nov 24 '18 at 9:15
















                        0














                        You Regex match string is incorrect.



                        Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$



                        See example here






                        share|improve this answer


























                        • You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.

                          – Pushpesh Kumar Rajwanshi
                          Nov 24 '18 at 9:04











                        • Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.

                          – Julo
                          Nov 24 '18 at 9:15














                        0












                        0








                        0







                        You Regex match string is incorrect.



                        Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$



                        See example here






                        share|improve this answer















                        You Regex match string is incorrect.



                        Try: ^[0-9]+((,[0-9]+)+)?(.[0-9]+)?$



                        See example here







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 24 '18 at 9:10

























                        answered Nov 24 '18 at 6:49









                        JuloJulo

                        8771815




                        8771815













                        • You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.

                          – Pushpesh Kumar Rajwanshi
                          Nov 24 '18 at 9:04











                        • Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.

                          – Julo
                          Nov 24 '18 at 9:15



















                        • You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.

                          – Pushpesh Kumar Rajwanshi
                          Nov 24 '18 at 9:04











                        • Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.

                          – Julo
                          Nov 24 '18 at 9:15

















                        You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.

                        – Pushpesh Kumar Rajwanshi
                        Nov 24 '18 at 9:04





                        You forgot to escape dot due to which it matches 10%2 or 10P2. Also you don't need to escape comma. With your usage of ^ and $ this regex will not be able to find multiple matches the way OP wants.

                        – Pushpesh Kumar Rajwanshi
                        Nov 24 '18 at 9:04













                        Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.

                        – Julo
                        Nov 24 '18 at 9:15





                        Thanks for pointing out the error. It seems I was in haste and removed the prefixing from the wrong character. It's fixed now.

                        – Julo
                        Nov 24 '18 at 9:15











                        0














                        In your regex you use anchors ^ to assert the start and the end $ of the string where USD is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.



                        To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*. Note that you don't have to escape the comma.



                        If the USD is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.



                        You could use named capturing group to reference the group by name.



                        bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb



                        That would match





                        • bUSD Match word boundary and USD


                        • (?<n1>d+) Named capturing group n1 which will match 1+ digits


                        • | or


                        • (?<n2> Start named capturing group n2



                          • d{1,3} Match 1-3 digits


                          • (?:,d{3})* Match a comma, 3 digits and repeat 0+ times


                          • (?:.d+)? Match a dot and 1+ digits and make that optional




                        • ) Close namedd capturing group


                        • USDb Match space, USD and a word boundary


                        Regex demo | C# demo






                        share|improve this answer




























                          0














                          In your regex you use anchors ^ to assert the start and the end $ of the string where USD is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.



                          To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*. Note that you don't have to escape the comma.



                          If the USD is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.



                          You could use named capturing group to reference the group by name.



                          bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb



                          That would match





                          • bUSD Match word boundary and USD


                          • (?<n1>d+) Named capturing group n1 which will match 1+ digits


                          • | or


                          • (?<n2> Start named capturing group n2



                            • d{1,3} Match 1-3 digits


                            • (?:,d{3})* Match a comma, 3 digits and repeat 0+ times


                            • (?:.d+)? Match a dot and 1+ digits and make that optional




                          • ) Close namedd capturing group


                          • USDb Match space, USD and a word boundary


                          Regex demo | C# demo






                          share|improve this answer


























                            0












                            0








                            0







                            In your regex you use anchors ^ to assert the start and the end $ of the string where USD is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.



                            To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*. Note that you don't have to escape the comma.



                            If the USD is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.



                            You could use named capturing group to reference the group by name.



                            bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb



                            That would match





                            • bUSD Match word boundary and USD


                            • (?<n1>d+) Named capturing group n1 which will match 1+ digits


                            • | or


                            • (?<n2> Start named capturing group n2



                              • d{1,3} Match 1-3 digits


                              • (?:,d{3})* Match a comma, 3 digits and repeat 0+ times


                              • (?:.d+)? Match a dot and 1+ digits and make that optional




                            • ) Close namedd capturing group


                            • USDb Match space, USD and a word boundary


                            Regex demo | C# demo






                            share|improve this answer













                            In your regex you use anchors ^ to assert the start and the end $ of the string where USD is not taken into consideration and would not match. If you want mulitple matches, you should use Regex.Matches instead.



                            To match a number without a dot or a comma, the middle part should match 0+ times as the last part is already optional and the digits in the character class need an quantifier to match 1+ times like (,[0-9]+)*. Note that you don't have to escape the comma.



                            If the USD is part of the match, you could use an alternation to match either USD1200 or 1,200.12 USD instead of matching all the digits.



                            You could use named capturing group to reference the group by name.



                            bUSD(?<n1>d+)|(?<n2>d{1,3}(?:,d{3})*(?:.d+)?) USDb



                            That would match





                            • bUSD Match word boundary and USD


                            • (?<n1>d+) Named capturing group n1 which will match 1+ digits


                            • | or


                            • (?<n2> Start named capturing group n2



                              • d{1,3} Match 1-3 digits


                              • (?:,d{3})* Match a comma, 3 digits and repeat 0+ times


                              • (?:.d+)? Match a dot and 1+ digits and make that optional




                            • ) Close namedd capturing group


                            • USDb Match space, USD and a word boundary


                            Regex demo | C# demo







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 10:29









                            The fourth birdThe fourth bird

                            22.9k81427




                            22.9k81427






























                                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%2f53455799%2fextracting-numeric-part-with-thousands-separator-and-decimal-point-using-regex-i%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

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                TypeError: fit_transform() missing 1 required positional argument: 'X'