How to split and keep numbers together












0















I am programming a math expression checker. I have this string:



Oper = "((234+332)+(cos4-sin65))"


I want to split this string by separating all "()"s and operators minus numbers or trigonometric ratios to get this result:



Oper = ['(', '(', '234', '+', '332', ')', '+', '(', 'cos4', '-', 'sin65', ')', ')']


How would the split be?










share|improve this question




















  • 1





    what is the logic of this split?

    – Lenin Raj Rajasekaran
    Nov 21 '18 at 22:37











  • The program is about math expressions checker and the idea is separate all the "()" and operators minus the numbers or trigonometric ratios

    – Alejandro Rodriguez
    Nov 21 '18 at 22:42






  • 1





    What's the logic of an input such as cos4 or sin65? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluating cosine(4) and sine(65)?

    – pjs
    Nov 21 '18 at 23:06











  • It is a bad idea to use constant for something like that.

    – sawa
    Nov 22 '18 at 5:07
















0















I am programming a math expression checker. I have this string:



Oper = "((234+332)+(cos4-sin65))"


I want to split this string by separating all "()"s and operators minus numbers or trigonometric ratios to get this result:



Oper = ['(', '(', '234', '+', '332', ')', '+', '(', 'cos4', '-', 'sin65', ')', ')']


How would the split be?










share|improve this question




















  • 1





    what is the logic of this split?

    – Lenin Raj Rajasekaran
    Nov 21 '18 at 22:37











  • The program is about math expressions checker and the idea is separate all the "()" and operators minus the numbers or trigonometric ratios

    – Alejandro Rodriguez
    Nov 21 '18 at 22:42






  • 1





    What's the logic of an input such as cos4 or sin65? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluating cosine(4) and sine(65)?

    – pjs
    Nov 21 '18 at 23:06











  • It is a bad idea to use constant for something like that.

    – sawa
    Nov 22 '18 at 5:07














0












0








0








I am programming a math expression checker. I have this string:



Oper = "((234+332)+(cos4-sin65))"


I want to split this string by separating all "()"s and operators minus numbers or trigonometric ratios to get this result:



Oper = ['(', '(', '234', '+', '332', ')', '+', '(', 'cos4', '-', 'sin65', ')', ')']


How would the split be?










share|improve this question
















I am programming a math expression checker. I have this string:



Oper = "((234+332)+(cos4-sin65))"


I want to split this string by separating all "()"s and operators minus numbers or trigonometric ratios to get this result:



Oper = ['(', '(', '234', '+', '332', ')', '+', '(', 'cos4', '-', 'sin65', ')', ')']


How would the split be?







ruby string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 5:11









sawa

131k29205303




131k29205303










asked Nov 21 '18 at 22:35









Alejandro RodriguezAlejandro Rodriguez

64




64








  • 1





    what is the logic of this split?

    – Lenin Raj Rajasekaran
    Nov 21 '18 at 22:37











  • The program is about math expressions checker and the idea is separate all the "()" and operators minus the numbers or trigonometric ratios

    – Alejandro Rodriguez
    Nov 21 '18 at 22:42






  • 1





    What's the logic of an input such as cos4 or sin65? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluating cosine(4) and sine(65)?

    – pjs
    Nov 21 '18 at 23:06











  • It is a bad idea to use constant for something like that.

    – sawa
    Nov 22 '18 at 5:07














  • 1





    what is the logic of this split?

    – Lenin Raj Rajasekaran
    Nov 21 '18 at 22:37











  • The program is about math expressions checker and the idea is separate all the "()" and operators minus the numbers or trigonometric ratios

    – Alejandro Rodriguez
    Nov 21 '18 at 22:42






  • 1





    What's the logic of an input such as cos4 or sin65? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluating cosine(4) and sine(65)?

    – pjs
    Nov 21 '18 at 23:06











  • It is a bad idea to use constant for something like that.

    – sawa
    Nov 22 '18 at 5:07








1




1





what is the logic of this split?

– Lenin Raj Rajasekaran
Nov 21 '18 at 22:37





what is the logic of this split?

– Lenin Raj Rajasekaran
Nov 21 '18 at 22:37













The program is about math expressions checker and the idea is separate all the "()" and operators minus the numbers or trigonometric ratios

– Alejandro Rodriguez
Nov 21 '18 at 22:42





The program is about math expressions checker and the idea is separate all the "()" and operators minus the numbers or trigonometric ratios

– Alejandro Rodriguez
Nov 21 '18 at 22:42




1




1





What's the logic of an input such as cos4 or sin65? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluating cosine(4) and sine(65)?

– pjs
Nov 21 '18 at 23:06





What's the logic of an input such as cos4 or sin65? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluating cosine(4) and sine(65)?

– pjs
Nov 21 '18 at 23:06













It is a bad idea to use constant for something like that.

– sawa
Nov 22 '18 at 5:07





It is a bad idea to use constant for something like that.

– sawa
Nov 22 '18 at 5:07












2 Answers
2






active

oldest

votes


















1














"((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
# => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


Splits the whole string by nothing or optional alphanumeric + digits.






share|improve this answer
























  • Thanks, is what I needed

    – Alejandro Rodriguez
    Nov 21 '18 at 22:53



















1














oper = "((234+332)+(cos4-sin65))"

oper.scan /[^[:alnum:]]|[[:alnum:]]+/
#=> ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


Scan for one non-alphanumeric character or one or more alphanumeric characters.






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%2f53421378%2fhow-to-split-and-keep-numbers-together%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














    "((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
    # => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


    Splits the whole string by nothing or optional alphanumeric + digits.






    share|improve this answer
























    • Thanks, is what I needed

      – Alejandro Rodriguez
      Nov 21 '18 at 22:53
















    1














    "((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
    # => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


    Splits the whole string by nothing or optional alphanumeric + digits.






    share|improve this answer
























    • Thanks, is what I needed

      – Alejandro Rodriguez
      Nov 21 '18 at 22:53














    1












    1








    1







    "((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
    # => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


    Splits the whole string by nothing or optional alphanumeric + digits.






    share|improve this answer













    "((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
    # => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


    Splits the whole string by nothing or optional alphanumeric + digits.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 22:50









    Marcin KołodziejMarcin Kołodziej

    4,4901315




    4,4901315













    • Thanks, is what I needed

      – Alejandro Rodriguez
      Nov 21 '18 at 22:53



















    • Thanks, is what I needed

      – Alejandro Rodriguez
      Nov 21 '18 at 22:53

















    Thanks, is what I needed

    – Alejandro Rodriguez
    Nov 21 '18 at 22:53





    Thanks, is what I needed

    – Alejandro Rodriguez
    Nov 21 '18 at 22:53













    1














    oper = "((234+332)+(cos4-sin65))"

    oper.scan /[^[:alnum:]]|[[:alnum:]]+/
    #=> ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


    Scan for one non-alphanumeric character or one or more alphanumeric characters.






    share|improve this answer




























      1














      oper = "((234+332)+(cos4-sin65))"

      oper.scan /[^[:alnum:]]|[[:alnum:]]+/
      #=> ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


      Scan for one non-alphanumeric character or one or more alphanumeric characters.






      share|improve this answer


























        1












        1








        1







        oper = "((234+332)+(cos4-sin65))"

        oper.scan /[^[:alnum:]]|[[:alnum:]]+/
        #=> ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


        Scan for one non-alphanumeric character or one or more alphanumeric characters.






        share|improve this answer













        oper = "((234+332)+(cos4-sin65))"

        oper.scan /[^[:alnum:]]|[[:alnum:]]+/
        #=> ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]


        Scan for one non-alphanumeric character or one or more alphanumeric characters.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 23:10









        Cary SwovelandCary Swoveland

        69.8k54166




        69.8k54166






























            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%2f53421378%2fhow-to-split-and-keep-numbers-together%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

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory