Regex to select latest expression in math formula to change sign
I want to have a regex that can detect last math expression in order to change sign of that expression for example :
5 --> -5
-5 --> 5
5+2*cos(10) --> 5-2*cos(10)
5-cos(10+2) --> 5+cos(10+2)
I still couldn't create any regex that can match last expression. I created this regex (+[(cos|sin|tan)])+(.*[0-9]*.) but it only select the last expression if there is a sin/cos or tan.
java regex kotlin
|
show 1 more comment
I want to have a regex that can detect last math expression in order to change sign of that expression for example :
5 --> -5
-5 --> 5
5+2*cos(10) --> 5-2*cos(10)
5-cos(10+2) --> 5+cos(10+2)
I still couldn't create any regex that can match last expression. I created this regex (+[(cos|sin|tan)])+(.*[0-9]*.) but it only select the last expression if there is a sin/cos or tan.
java regex kotlin
5
I think that regular expressions won't solve the whole problem. You need to build a tree repesenting all the operations to choose the last one.
– mrzasa
Nov 15 '18 at 14:52
hmm okay thank you. I thought maybe I'm missing some feature of regex that might help.
– AndroLife
Nov 15 '18 at 15:05
1
It will be a crazy thing to do, but the only regex way I see is to build a regex with alternatives matching and capturing various contexts and in the match evaluator part analyze the match structure and replace accordingly. A regex that probably handles all contexts is(?:cos|sin|tan)([^()]*)|^(-)|^(d)|([-+])
– Wiktor Stribiżew
Nov 15 '18 at 15:53
@WiktorStribiżew thanks I'm not really obliged to follow regex I will try tree parsing.
– AndroLife
Nov 15 '18 at 15:59
Why regex? You can sort and take the last entry.
– Dominique
Nov 15 '18 at 16:08
|
show 1 more comment
I want to have a regex that can detect last math expression in order to change sign of that expression for example :
5 --> -5
-5 --> 5
5+2*cos(10) --> 5-2*cos(10)
5-cos(10+2) --> 5+cos(10+2)
I still couldn't create any regex that can match last expression. I created this regex (+[(cos|sin|tan)])+(.*[0-9]*.) but it only select the last expression if there is a sin/cos or tan.
java regex kotlin
I want to have a regex that can detect last math expression in order to change sign of that expression for example :
5 --> -5
-5 --> 5
5+2*cos(10) --> 5-2*cos(10)
5-cos(10+2) --> 5+cos(10+2)
I still couldn't create any regex that can match last expression. I created this regex (+[(cos|sin|tan)])+(.*[0-9]*.) but it only select the last expression if there is a sin/cos or tan.
java regex kotlin
java regex kotlin
asked Nov 15 '18 at 14:49
AndroLifeAndroLife
4961821
4961821
5
I think that regular expressions won't solve the whole problem. You need to build a tree repesenting all the operations to choose the last one.
– mrzasa
Nov 15 '18 at 14:52
hmm okay thank you. I thought maybe I'm missing some feature of regex that might help.
– AndroLife
Nov 15 '18 at 15:05
1
It will be a crazy thing to do, but the only regex way I see is to build a regex with alternatives matching and capturing various contexts and in the match evaluator part analyze the match structure and replace accordingly. A regex that probably handles all contexts is(?:cos|sin|tan)([^()]*)|^(-)|^(d)|([-+])
– Wiktor Stribiżew
Nov 15 '18 at 15:53
@WiktorStribiżew thanks I'm not really obliged to follow regex I will try tree parsing.
– AndroLife
Nov 15 '18 at 15:59
Why regex? You can sort and take the last entry.
– Dominique
Nov 15 '18 at 16:08
|
show 1 more comment
5
I think that regular expressions won't solve the whole problem. You need to build a tree repesenting all the operations to choose the last one.
– mrzasa
Nov 15 '18 at 14:52
hmm okay thank you. I thought maybe I'm missing some feature of regex that might help.
– AndroLife
Nov 15 '18 at 15:05
1
It will be a crazy thing to do, but the only regex way I see is to build a regex with alternatives matching and capturing various contexts and in the match evaluator part analyze the match structure and replace accordingly. A regex that probably handles all contexts is(?:cos|sin|tan)([^()]*)|^(-)|^(d)|([-+])
– Wiktor Stribiżew
Nov 15 '18 at 15:53
@WiktorStribiżew thanks I'm not really obliged to follow regex I will try tree parsing.
– AndroLife
Nov 15 '18 at 15:59
Why regex? You can sort and take the last entry.
– Dominique
Nov 15 '18 at 16:08
5
5
I think that regular expressions won't solve the whole problem. You need to build a tree repesenting all the operations to choose the last one.
– mrzasa
Nov 15 '18 at 14:52
I think that regular expressions won't solve the whole problem. You need to build a tree repesenting all the operations to choose the last one.
– mrzasa
Nov 15 '18 at 14:52
hmm okay thank you. I thought maybe I'm missing some feature of regex that might help.
– AndroLife
Nov 15 '18 at 15:05
hmm okay thank you. I thought maybe I'm missing some feature of regex that might help.
– AndroLife
Nov 15 '18 at 15:05
1
1
It will be a crazy thing to do, but the only regex way I see is to build a regex with alternatives matching and capturing various contexts and in the match evaluator part analyze the match structure and replace accordingly. A regex that probably handles all contexts is
(?:cos|sin|tan)([^()]*)|^(-)|^(d)|([-+])– Wiktor Stribiżew
Nov 15 '18 at 15:53
It will be a crazy thing to do, but the only regex way I see is to build a regex with alternatives matching and capturing various contexts and in the match evaluator part analyze the match structure and replace accordingly. A regex that probably handles all contexts is
(?:cos|sin|tan)([^()]*)|^(-)|^(d)|([-+])– Wiktor Stribiżew
Nov 15 '18 at 15:53
@WiktorStribiżew thanks I'm not really obliged to follow regex I will try tree parsing.
– AndroLife
Nov 15 '18 at 15:59
@WiktorStribiżew thanks I'm not really obliged to follow regex I will try tree parsing.
– AndroLife
Nov 15 '18 at 15:59
Why regex? You can sort and take the last entry.
– Dominique
Nov 15 '18 at 16:08
Why regex? You can sort and take the last entry.
– Dominique
Nov 15 '18 at 16:08
|
show 1 more comment
1 Answer
1
active
oldest
votes
Sorry for being (incredibly) late to the party, but here's my solution:
String str = "5*10/2+cos(54/90)", regex = "(\+|-)(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$)";
Pattern pat = Pattern.compile(regex);
Matcher m = pat.matcher(str);
String r = "";
if(m.find())
{
r = m.group(1).equals("+")?"-":"+";
}
str = str.replaceAll(regex, r);
System.out.println(str);
I'm using BlueJ as an IDE, btw.
Here's a live demo: https://regex101.com/r/3EM9Ix/1
EXPLANATION
(\+|-) selects the desired operator, '+' or '-'
(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$) is the regex that checks if the operator is followed by either a function which has letters in it or just some more digits
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%2f53322053%2fregex-to-select-latest-expression-in-math-formula-to-change-sign%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
Sorry for being (incredibly) late to the party, but here's my solution:
String str = "5*10/2+cos(54/90)", regex = "(\+|-)(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$)";
Pattern pat = Pattern.compile(regex);
Matcher m = pat.matcher(str);
String r = "";
if(m.find())
{
r = m.group(1).equals("+")?"-":"+";
}
str = str.replaceAll(regex, r);
System.out.println(str);
I'm using BlueJ as an IDE, btw.
Here's a live demo: https://regex101.com/r/3EM9Ix/1
EXPLANATION
(\+|-) selects the desired operator, '+' or '-'
(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$) is the regex that checks if the operator is followed by either a function which has letters in it or just some more digits
add a comment |
Sorry for being (incredibly) late to the party, but here's my solution:
String str = "5*10/2+cos(54/90)", regex = "(\+|-)(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$)";
Pattern pat = Pattern.compile(regex);
Matcher m = pat.matcher(str);
String r = "";
if(m.find())
{
r = m.group(1).equals("+")?"-":"+";
}
str = str.replaceAll(regex, r);
System.out.println(str);
I'm using BlueJ as an IDE, btw.
Here's a live demo: https://regex101.com/r/3EM9Ix/1
EXPLANATION
(\+|-) selects the desired operator, '+' or '-'
(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$) is the regex that checks if the operator is followed by either a function which has letters in it or just some more digits
add a comment |
Sorry for being (incredibly) late to the party, but here's my solution:
String str = "5*10/2+cos(54/90)", regex = "(\+|-)(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$)";
Pattern pat = Pattern.compile(regex);
Matcher m = pat.matcher(str);
String r = "";
if(m.find())
{
r = m.group(1).equals("+")?"-":"+";
}
str = str.replaceAll(regex, r);
System.out.println(str);
I'm using BlueJ as an IDE, btw.
Here's a live demo: https://regex101.com/r/3EM9Ix/1
EXPLANATION
(\+|-) selects the desired operator, '+' or '-'
(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$) is the regex that checks if the operator is followed by either a function which has letters in it or just some more digits
Sorry for being (incredibly) late to the party, but here's my solution:
String str = "5*10/2+cos(54/90)", regex = "(\+|-)(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$)";
Pattern pat = Pattern.compile(regex);
Matcher m = pat.matcher(str);
String r = "";
if(m.find())
{
r = m.group(1).equals("+")?"-":"+";
}
str = str.replaceAll(regex, r);
System.out.println(str);
I'm using BlueJ as an IDE, btw.
Here's a live demo: https://regex101.com/r/3EM9Ix/1
EXPLANATION
(\+|-) selects the desired operator, '+' or '-'
(?=(?:[a-zA-Z]*\(.*?\))|[0-9^*/]+$) is the regex that checks if the operator is followed by either a function which has letters in it or just some more digits
answered Jan 13 at 19:48
Robo MopRobo Mop
1,9061517
1,9061517
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%2f53322053%2fregex-to-select-latest-expression-in-math-formula-to-change-sign%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
5
I think that regular expressions won't solve the whole problem. You need to build a tree repesenting all the operations to choose the last one.
– mrzasa
Nov 15 '18 at 14:52
hmm okay thank you. I thought maybe I'm missing some feature of regex that might help.
– AndroLife
Nov 15 '18 at 15:05
1
It will be a crazy thing to do, but the only regex way I see is to build a regex with alternatives matching and capturing various contexts and in the match evaluator part analyze the match structure and replace accordingly. A regex that probably handles all contexts is
(?:cos|sin|tan)([^()]*)|^(-)|^(d)|([-+])– Wiktor Stribiżew
Nov 15 '18 at 15:53
@WiktorStribiżew thanks I'm not really obliged to follow regex I will try tree parsing.
– AndroLife
Nov 15 '18 at 15:59
Why regex? You can sort and take the last entry.
– Dominique
Nov 15 '18 at 16:08