Matching free terms in a polynom with real coefficients using regexMatch all occurrences of a regexEfficiency of Java “Double Brace Initialization”?RegEx match open tags except XHTML self-contained tagsCheck whether a string matches a regex in JSWhy it's not possible to use regex to parse HTML/XML: a formal explanation in layman's termsregex: Match at least two search termsMatching algebraic termsnot matching empty string for regexHow to match a regex only if it matches exactlyUsing regex to match numbers which have 5 increasing consecutive digits somewhere in them

Could solar power be utilized and substitute coal in the 19th Century

How do I extrude a face to a single vertex

Why does Async/Await work properly when the loop is inside the async function and not the other way around?

We have a love-hate relationship

Does the Mind Blank spell prevent the target from being frightened?

What's the difference between 違法 and 不法?

How should I respond when I lied about my education and the company finds out through background check?

Bob has never been a M before

If a character with the Alert feat rolls a crit fail on their Perception check, are they surprised?

How can "mimic phobia" be cured or prevented?

Why is Arduino resetting while driving motors?

Open a doc from terminal, but not by its name

Is camera lens focus an exact point or a range?

What (else) happened July 1st 1858 in London?

Can a significant change in incentives void an employment contract?

Did arcade monitors have same pixel aspect ratio as TV sets?

Should I stop contributing to retirement accounts?

How much character growth crosses the line into breaking the character

Can we have a perfect cadence in a minor key?

Create all possible words using a set or letters

Is it possible to have a strip of cold climate in the middle of a planet?

Is there a word to describe the feeling of being transfixed out of horror?

Could the E-bike drivetrain wear down till needing replacement after 400 km?

Why has "pence" been used in this sentence, not "pences"?



Matching free terms in a polynom with real coefficients using regex


Match all occurrences of a regexEfficiency of Java “Double Brace Initialization”?RegEx match open tags except XHTML self-contained tagsCheck whether a string matches a regex in JSWhy it's not possible to use regex to parse HTML/XML: a formal explanation in layman's termsregex: Match at least two search termsMatching algebraic termsnot matching empty string for regexHow to match a regex only if it matches exactlyUsing regex to match numbers which have 5 increasing consecutive digits somewhere in them













0















I have to make an assignment for Programming Techniques and I have struggled to match the free terms in a polynomial, (2.0x^2+3.5x-3.0 or 45x+2), which is given as a string. I have tried to use the following regex:



(-0,1d+(.d+)0,1)(?![0-9].*x^d))


I know the way the way I approached it might not be too good, but I did not have too much time for understanding regex in a more complex way, only slightly, and I got stuck at the point in which I would like to get only the numbers like with the format: ±# or ±#.# which are not followed by: x or x^# where # represents one or more digits.










share|improve this question



















  • 1





    Is it always the last term? Because that would make things much easier.

    – Aaron
    Mar 21 at 13:15











  • No, actually it can be anywhere in the polynomial, I forgot to mention that..

    – Manuel Maior
    Mar 21 at 13:21












  • What language is this regex used in? The environment makes a difference with regex.

    – Adam
    Mar 21 at 13:22












  • The regex is used in Java, and for testing I have used this site: regexr.com/4amm9, and for other patterns it had worked fine

    – Manuel Maior
    Mar 21 at 13:22












  • Are you asked to solve the whole thing in regex, or can you use a bit of Java plumbing? Because (?<!^)[-+]?d+(.d+)?(?=$|[-+]) works, but I doubt that's what you're asked for

    – Aaron
    Mar 21 at 13:25















0















I have to make an assignment for Programming Techniques and I have struggled to match the free terms in a polynomial, (2.0x^2+3.5x-3.0 or 45x+2), which is given as a string. I have tried to use the following regex:



(-0,1d+(.d+)0,1)(?![0-9].*x^d))


I know the way the way I approached it might not be too good, but I did not have too much time for understanding regex in a more complex way, only slightly, and I got stuck at the point in which I would like to get only the numbers like with the format: ±# or ±#.# which are not followed by: x or x^# where # represents one or more digits.










share|improve this question



















  • 1





    Is it always the last term? Because that would make things much easier.

    – Aaron
    Mar 21 at 13:15











  • No, actually it can be anywhere in the polynomial, I forgot to mention that..

    – Manuel Maior
    Mar 21 at 13:21












  • What language is this regex used in? The environment makes a difference with regex.

    – Adam
    Mar 21 at 13:22












  • The regex is used in Java, and for testing I have used this site: regexr.com/4amm9, and for other patterns it had worked fine

    – Manuel Maior
    Mar 21 at 13:22












  • Are you asked to solve the whole thing in regex, or can you use a bit of Java plumbing? Because (?<!^)[-+]?d+(.d+)?(?=$|[-+]) works, but I doubt that's what you're asked for

    – Aaron
    Mar 21 at 13:25













0












0








0








I have to make an assignment for Programming Techniques and I have struggled to match the free terms in a polynomial, (2.0x^2+3.5x-3.0 or 45x+2), which is given as a string. I have tried to use the following regex:



(-0,1d+(.d+)0,1)(?![0-9].*x^d))


I know the way the way I approached it might not be too good, but I did not have too much time for understanding regex in a more complex way, only slightly, and I got stuck at the point in which I would like to get only the numbers like with the format: ±# or ±#.# which are not followed by: x or x^# where # represents one or more digits.










share|improve this question
















I have to make an assignment for Programming Techniques and I have struggled to match the free terms in a polynomial, (2.0x^2+3.5x-3.0 or 45x+2), which is given as a string. I have tried to use the following regex:



(-0,1d+(.d+)0,1)(?![0-9].*x^d))


I know the way the way I approached it might not be too good, but I did not have too much time for understanding regex in a more complex way, only slightly, and I got stuck at the point in which I would like to get only the numbers like with the format: ±# or ±#.# which are not followed by: x or x^# where # represents one or more digits.







java regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 13:24









Adam

1,615719




1,615719










asked Mar 21 at 13:10









Manuel MaiorManuel Maior

33




33







  • 1





    Is it always the last term? Because that would make things much easier.

    – Aaron
    Mar 21 at 13:15











  • No, actually it can be anywhere in the polynomial, I forgot to mention that..

    – Manuel Maior
    Mar 21 at 13:21












  • What language is this regex used in? The environment makes a difference with regex.

    – Adam
    Mar 21 at 13:22












  • The regex is used in Java, and for testing I have used this site: regexr.com/4amm9, and for other patterns it had worked fine

    – Manuel Maior
    Mar 21 at 13:22












  • Are you asked to solve the whole thing in regex, or can you use a bit of Java plumbing? Because (?<!^)[-+]?d+(.d+)?(?=$|[-+]) works, but I doubt that's what you're asked for

    – Aaron
    Mar 21 at 13:25












  • 1





    Is it always the last term? Because that would make things much easier.

    – Aaron
    Mar 21 at 13:15











  • No, actually it can be anywhere in the polynomial, I forgot to mention that..

    – Manuel Maior
    Mar 21 at 13:21












  • What language is this regex used in? The environment makes a difference with regex.

    – Adam
    Mar 21 at 13:22












  • The regex is used in Java, and for testing I have used this site: regexr.com/4amm9, and for other patterns it had worked fine

    – Manuel Maior
    Mar 21 at 13:22












  • Are you asked to solve the whole thing in regex, or can you use a bit of Java plumbing? Because (?<!^)[-+]?d+(.d+)?(?=$|[-+]) works, but I doubt that's what you're asked for

    – Aaron
    Mar 21 at 13:25







1




1





Is it always the last term? Because that would make things much easier.

– Aaron
Mar 21 at 13:15





Is it always the last term? Because that would make things much easier.

– Aaron
Mar 21 at 13:15













No, actually it can be anywhere in the polynomial, I forgot to mention that..

– Manuel Maior
Mar 21 at 13:21






No, actually it can be anywhere in the polynomial, I forgot to mention that..

– Manuel Maior
Mar 21 at 13:21














What language is this regex used in? The environment makes a difference with regex.

– Adam
Mar 21 at 13:22






What language is this regex used in? The environment makes a difference with regex.

– Adam
Mar 21 at 13:22














The regex is used in Java, and for testing I have used this site: regexr.com/4amm9, and for other patterns it had worked fine

– Manuel Maior
Mar 21 at 13:22






The regex is used in Java, and for testing I have used this site: regexr.com/4amm9, and for other patterns it had worked fine

– Manuel Maior
Mar 21 at 13:22














Are you asked to solve the whole thing in regex, or can you use a bit of Java plumbing? Because (?<!^)[-+]?d+(.d+)?(?=$|[-+]) works, but I doubt that's what you're asked for

– Aaron
Mar 21 at 13:25





Are you asked to solve the whole thing in regex, or can you use a bit of Java plumbing? Because (?<!^)[-+]?d+(.d+)?(?=$|[-+]) works, but I doubt that's what you're asked for

– Aaron
Mar 21 at 13:25












2 Answers
2






active

oldest

votes


















0














Full regex solution :



(?<!^)[-+]?d+(.d+)?(?=$|[-+])



  • (?<!^) won't match if the previous character is a ^


  • [-+]? will match an optional sign character


  • d+(.d+)? will match the number which is composed of an integer part followed by an optional floating part


  • (?=$|[-+]) won't match unless what follows is a sign character or the end of the string

You can try it here.




Java solution with term-matching :



Pattern termPattern = Pattern.compile("\d+(?:\.\d+)?(x(?:\^\d+)?)?");
Matcher termMatcher = termPattern.matcher(input);
while (termMatcher.find())
if (termMatcher.group(1) == null)
// you have a free term




The regex matches an integer part followed by an optional floating part then an optional x^n part which is captured in a capturing group. A Matcher is created from applying the pattern to the input. Calling Matcher.find allows us to iterate over the multiple matches on the input string. For each match we check the content of the first capturing group, when it's null we have a free term.



You can try it here.




Yet anoter solution would be to simply split the string around [+-] and for each part test whether it contains x or not.






share|improve this answer

























  • Yes, finally I have tested the solution you have given and it seems to work good. Also thank you for explaining each part in the solution. It is really clear now.

    – Manuel Maior
    Mar 21 at 13:47











  • @ManuelMaior you're welcome ! Note that lookarounds (positive/negative lookafter/lookbehind) are an advanced feature of regex which you usually don't learn right away, so if you haven't explored regexs that much I think your teacher would rather expect something like the second solution.

    – Aaron
    Mar 21 at 13:50












  • Yes, the second solution might seem more simple and it also has a good logic.

    – Manuel Maior
    Mar 21 at 13:52











  • I've edited the second regex to make the exponent part of x^n optional. This doesn't change your result since you don't care about the results with an x^n part for now, but if you're made to capture every term it is necessary (so would matching terms without coefficient such as the lone x in your sample input, but I'll leave that part to you)

    – Aaron
    Mar 21 at 14:01



















0














This will do it: (?<!^)(d+(?:.d+)?)(?![x.d])



(?<! | Negative lookbehind:
^ | A literal "^"
) | Close group
( | Capture the following:
d+ | Match one or more digits
(?: | Match the following group:
. | A literal "."
d+ | One or more digits
)? | Close group, optional match
) | End capture
(?! | Negative lookahead:
[x.d] | Either an "x", ".", or any digit
) | Close group


Try it here




String s = "5x-50.1+x^2-2+20x";
Pattern p = Pattern.compile("(?<!\^)(\d+(?:\.\d+)?)(?![x.\d])");
Matcher m = p.matcher(s);
if (m.find())
System.out.println(m.group(1)); // Output: 50.1






share|improve this answer























  • thank you for this alternative solution and for the explainations!

    – Manuel Maior
    Mar 21 at 14:07










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%2f55281196%2fmatching-free-terms-in-a-polynom-with-real-coefficients-using-regex%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









0














Full regex solution :



(?<!^)[-+]?d+(.d+)?(?=$|[-+])



  • (?<!^) won't match if the previous character is a ^


  • [-+]? will match an optional sign character


  • d+(.d+)? will match the number which is composed of an integer part followed by an optional floating part


  • (?=$|[-+]) won't match unless what follows is a sign character or the end of the string

You can try it here.




Java solution with term-matching :



Pattern termPattern = Pattern.compile("\d+(?:\.\d+)?(x(?:\^\d+)?)?");
Matcher termMatcher = termPattern.matcher(input);
while (termMatcher.find())
if (termMatcher.group(1) == null)
// you have a free term




The regex matches an integer part followed by an optional floating part then an optional x^n part which is captured in a capturing group. A Matcher is created from applying the pattern to the input. Calling Matcher.find allows us to iterate over the multiple matches on the input string. For each match we check the content of the first capturing group, when it's null we have a free term.



You can try it here.




Yet anoter solution would be to simply split the string around [+-] and for each part test whether it contains x or not.






share|improve this answer

























  • Yes, finally I have tested the solution you have given and it seems to work good. Also thank you for explaining each part in the solution. It is really clear now.

    – Manuel Maior
    Mar 21 at 13:47











  • @ManuelMaior you're welcome ! Note that lookarounds (positive/negative lookafter/lookbehind) are an advanced feature of regex which you usually don't learn right away, so if you haven't explored regexs that much I think your teacher would rather expect something like the second solution.

    – Aaron
    Mar 21 at 13:50












  • Yes, the second solution might seem more simple and it also has a good logic.

    – Manuel Maior
    Mar 21 at 13:52











  • I've edited the second regex to make the exponent part of x^n optional. This doesn't change your result since you don't care about the results with an x^n part for now, but if you're made to capture every term it is necessary (so would matching terms without coefficient such as the lone x in your sample input, but I'll leave that part to you)

    – Aaron
    Mar 21 at 14:01
















0














Full regex solution :



(?<!^)[-+]?d+(.d+)?(?=$|[-+])



  • (?<!^) won't match if the previous character is a ^


  • [-+]? will match an optional sign character


  • d+(.d+)? will match the number which is composed of an integer part followed by an optional floating part


  • (?=$|[-+]) won't match unless what follows is a sign character or the end of the string

You can try it here.




Java solution with term-matching :



Pattern termPattern = Pattern.compile("\d+(?:\.\d+)?(x(?:\^\d+)?)?");
Matcher termMatcher = termPattern.matcher(input);
while (termMatcher.find())
if (termMatcher.group(1) == null)
// you have a free term




The regex matches an integer part followed by an optional floating part then an optional x^n part which is captured in a capturing group. A Matcher is created from applying the pattern to the input. Calling Matcher.find allows us to iterate over the multiple matches on the input string. For each match we check the content of the first capturing group, when it's null we have a free term.



You can try it here.




Yet anoter solution would be to simply split the string around [+-] and for each part test whether it contains x or not.






share|improve this answer

























  • Yes, finally I have tested the solution you have given and it seems to work good. Also thank you for explaining each part in the solution. It is really clear now.

    – Manuel Maior
    Mar 21 at 13:47











  • @ManuelMaior you're welcome ! Note that lookarounds (positive/negative lookafter/lookbehind) are an advanced feature of regex which you usually don't learn right away, so if you haven't explored regexs that much I think your teacher would rather expect something like the second solution.

    – Aaron
    Mar 21 at 13:50












  • Yes, the second solution might seem more simple and it also has a good logic.

    – Manuel Maior
    Mar 21 at 13:52











  • I've edited the second regex to make the exponent part of x^n optional. This doesn't change your result since you don't care about the results with an x^n part for now, but if you're made to capture every term it is necessary (so would matching terms without coefficient such as the lone x in your sample input, but I'll leave that part to you)

    – Aaron
    Mar 21 at 14:01














0












0








0







Full regex solution :



(?<!^)[-+]?d+(.d+)?(?=$|[-+])



  • (?<!^) won't match if the previous character is a ^


  • [-+]? will match an optional sign character


  • d+(.d+)? will match the number which is composed of an integer part followed by an optional floating part


  • (?=$|[-+]) won't match unless what follows is a sign character or the end of the string

You can try it here.




Java solution with term-matching :



Pattern termPattern = Pattern.compile("\d+(?:\.\d+)?(x(?:\^\d+)?)?");
Matcher termMatcher = termPattern.matcher(input);
while (termMatcher.find())
if (termMatcher.group(1) == null)
// you have a free term




The regex matches an integer part followed by an optional floating part then an optional x^n part which is captured in a capturing group. A Matcher is created from applying the pattern to the input. Calling Matcher.find allows us to iterate over the multiple matches on the input string. For each match we check the content of the first capturing group, when it's null we have a free term.



You can try it here.




Yet anoter solution would be to simply split the string around [+-] and for each part test whether it contains x or not.






share|improve this answer















Full regex solution :



(?<!^)[-+]?d+(.d+)?(?=$|[-+])



  • (?<!^) won't match if the previous character is a ^


  • [-+]? will match an optional sign character


  • d+(.d+)? will match the number which is composed of an integer part followed by an optional floating part


  • (?=$|[-+]) won't match unless what follows is a sign character or the end of the string

You can try it here.




Java solution with term-matching :



Pattern termPattern = Pattern.compile("\d+(?:\.\d+)?(x(?:\^\d+)?)?");
Matcher termMatcher = termPattern.matcher(input);
while (termMatcher.find())
if (termMatcher.group(1) == null)
// you have a free term




The regex matches an integer part followed by an optional floating part then an optional x^n part which is captured in a capturing group. A Matcher is created from applying the pattern to the input. Calling Matcher.find allows us to iterate over the multiple matches on the input string. For each match we check the content of the first capturing group, when it's null we have a free term.



You can try it here.




Yet anoter solution would be to simply split the string around [+-] and for each part test whether it contains x or not.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 21 at 13:57

























answered Mar 21 at 13:38









AaronAaron

16.1k11636




16.1k11636












  • Yes, finally I have tested the solution you have given and it seems to work good. Also thank you for explaining each part in the solution. It is really clear now.

    – Manuel Maior
    Mar 21 at 13:47











  • @ManuelMaior you're welcome ! Note that lookarounds (positive/negative lookafter/lookbehind) are an advanced feature of regex which you usually don't learn right away, so if you haven't explored regexs that much I think your teacher would rather expect something like the second solution.

    – Aaron
    Mar 21 at 13:50












  • Yes, the second solution might seem more simple and it also has a good logic.

    – Manuel Maior
    Mar 21 at 13:52











  • I've edited the second regex to make the exponent part of x^n optional. This doesn't change your result since you don't care about the results with an x^n part for now, but if you're made to capture every term it is necessary (so would matching terms without coefficient such as the lone x in your sample input, but I'll leave that part to you)

    – Aaron
    Mar 21 at 14:01


















  • Yes, finally I have tested the solution you have given and it seems to work good. Also thank you for explaining each part in the solution. It is really clear now.

    – Manuel Maior
    Mar 21 at 13:47











  • @ManuelMaior you're welcome ! Note that lookarounds (positive/negative lookafter/lookbehind) are an advanced feature of regex which you usually don't learn right away, so if you haven't explored regexs that much I think your teacher would rather expect something like the second solution.

    – Aaron
    Mar 21 at 13:50












  • Yes, the second solution might seem more simple and it also has a good logic.

    – Manuel Maior
    Mar 21 at 13:52











  • I've edited the second regex to make the exponent part of x^n optional. This doesn't change your result since you don't care about the results with an x^n part for now, but if you're made to capture every term it is necessary (so would matching terms without coefficient such as the lone x in your sample input, but I'll leave that part to you)

    – Aaron
    Mar 21 at 14:01

















Yes, finally I have tested the solution you have given and it seems to work good. Also thank you for explaining each part in the solution. It is really clear now.

– Manuel Maior
Mar 21 at 13:47





Yes, finally I have tested the solution you have given and it seems to work good. Also thank you for explaining each part in the solution. It is really clear now.

– Manuel Maior
Mar 21 at 13:47













@ManuelMaior you're welcome ! Note that lookarounds (positive/negative lookafter/lookbehind) are an advanced feature of regex which you usually don't learn right away, so if you haven't explored regexs that much I think your teacher would rather expect something like the second solution.

– Aaron
Mar 21 at 13:50






@ManuelMaior you're welcome ! Note that lookarounds (positive/negative lookafter/lookbehind) are an advanced feature of regex which you usually don't learn right away, so if you haven't explored regexs that much I think your teacher would rather expect something like the second solution.

– Aaron
Mar 21 at 13:50














Yes, the second solution might seem more simple and it also has a good logic.

– Manuel Maior
Mar 21 at 13:52





Yes, the second solution might seem more simple and it also has a good logic.

– Manuel Maior
Mar 21 at 13:52













I've edited the second regex to make the exponent part of x^n optional. This doesn't change your result since you don't care about the results with an x^n part for now, but if you're made to capture every term it is necessary (so would matching terms without coefficient such as the lone x in your sample input, but I'll leave that part to you)

– Aaron
Mar 21 at 14:01






I've edited the second regex to make the exponent part of x^n optional. This doesn't change your result since you don't care about the results with an x^n part for now, but if you're made to capture every term it is necessary (so would matching terms without coefficient such as the lone x in your sample input, but I'll leave that part to you)

– Aaron
Mar 21 at 14:01














0














This will do it: (?<!^)(d+(?:.d+)?)(?![x.d])



(?<! | Negative lookbehind:
^ | A literal "^"
) | Close group
( | Capture the following:
d+ | Match one or more digits
(?: | Match the following group:
. | A literal "."
d+ | One or more digits
)? | Close group, optional match
) | End capture
(?! | Negative lookahead:
[x.d] | Either an "x", ".", or any digit
) | Close group


Try it here




String s = "5x-50.1+x^2-2+20x";
Pattern p = Pattern.compile("(?<!\^)(\d+(?:\.\d+)?)(?![x.\d])");
Matcher m = p.matcher(s);
if (m.find())
System.out.println(m.group(1)); // Output: 50.1






share|improve this answer























  • thank you for this alternative solution and for the explainations!

    – Manuel Maior
    Mar 21 at 14:07















0














This will do it: (?<!^)(d+(?:.d+)?)(?![x.d])



(?<! | Negative lookbehind:
^ | A literal "^"
) | Close group
( | Capture the following:
d+ | Match one or more digits
(?: | Match the following group:
. | A literal "."
d+ | One or more digits
)? | Close group, optional match
) | End capture
(?! | Negative lookahead:
[x.d] | Either an "x", ".", or any digit
) | Close group


Try it here




String s = "5x-50.1+x^2-2+20x";
Pattern p = Pattern.compile("(?<!\^)(\d+(?:\.\d+)?)(?![x.\d])");
Matcher m = p.matcher(s);
if (m.find())
System.out.println(m.group(1)); // Output: 50.1






share|improve this answer























  • thank you for this alternative solution and for the explainations!

    – Manuel Maior
    Mar 21 at 14:07













0












0








0







This will do it: (?<!^)(d+(?:.d+)?)(?![x.d])



(?<! | Negative lookbehind:
^ | A literal "^"
) | Close group
( | Capture the following:
d+ | Match one or more digits
(?: | Match the following group:
. | A literal "."
d+ | One or more digits
)? | Close group, optional match
) | End capture
(?! | Negative lookahead:
[x.d] | Either an "x", ".", or any digit
) | Close group


Try it here




String s = "5x-50.1+x^2-2+20x";
Pattern p = Pattern.compile("(?<!\^)(\d+(?:\.\d+)?)(?![x.\d])");
Matcher m = p.matcher(s);
if (m.find())
System.out.println(m.group(1)); // Output: 50.1






share|improve this answer













This will do it: (?<!^)(d+(?:.d+)?)(?![x.d])



(?<! | Negative lookbehind:
^ | A literal "^"
) | Close group
( | Capture the following:
d+ | Match one or more digits
(?: | Match the following group:
. | A literal "."
d+ | One or more digits
)? | Close group, optional match
) | End capture
(?! | Negative lookahead:
[x.d] | Either an "x", ".", or any digit
) | Close group


Try it here




String s = "5x-50.1+x^2-2+20x";
Pattern p = Pattern.compile("(?<!\^)(\d+(?:\.\d+)?)(?![x.\d])");
Matcher m = p.matcher(s);
if (m.find())
System.out.println(m.group(1)); // Output: 50.1







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 13:59









AdamAdam

1,615719




1,615719












  • thank you for this alternative solution and for the explainations!

    – Manuel Maior
    Mar 21 at 14:07

















  • thank you for this alternative solution and for the explainations!

    – Manuel Maior
    Mar 21 at 14:07
















thank you for this alternative solution and for the explainations!

– Manuel Maior
Mar 21 at 14:07





thank you for this alternative solution and for the explainations!

– Manuel Maior
Mar 21 at 14:07

















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%2f55281196%2fmatching-free-terms-in-a-polynom-with-real-coefficients-using-regex%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현