How to split and keep numbers together
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
add a comment |
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
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 ascos4
orsin65
? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluatingcosine(4)
andsine(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
add a comment |
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
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
ruby string
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 ascos4
orsin65
? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluatingcosine(4)
andsine(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
add a comment |
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 ascos4
orsin65
? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluatingcosine(4)
andsine(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
add a comment |
2 Answers
2
active
oldest
votes
"((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
# => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]
Splits the whole string by nothing or optional alphanumeric + digits.
Thanks, is what I needed
– Alejandro Rodriguez
Nov 21 '18 at 22:53
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
"((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
# => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]
Splits the whole string by nothing or optional alphanumeric + digits.
Thanks, is what I needed
– Alejandro Rodriguez
Nov 21 '18 at 22:53
add a comment |
"((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
# => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]
Splits the whole string by nothing or optional alphanumeric + digits.
Thanks, is what I needed
– Alejandro Rodriguez
Nov 21 '18 at 22:53
add a comment |
"((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
# => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]
Splits the whole string by nothing or optional alphanumeric + digits.
"((234+332)+(cos4-sin65))".split /([[:alpha:]]*d+)*/
# => ["(", "(", "234", "+", "332", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]
Splits the whole string by nothing or optional alphanumeric + digits.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 23:10
Cary SwovelandCary Swoveland
69.8k54166
69.8k54166
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
orsin65
? You seem to be treating them as labels or variable names. How would one know that's not supposed to be evaluatingcosine(4)
andsine(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