0

When I write: "integralfrom1to10ofx^2)+integralfrom1to10ofx^3)",

I expect my regex:

// INTEGRAL BASIC
Pattern integralFinalPattern = Pattern.compile("integralfrom(.*)to(.*)of(.*)\\)");
Matcher integralFinalMatcher = integralFinalPattern.matcher(regexDedicatedString);
if(integralFinalMatcher.find()){
    String integral_user_input = integralFinalMatcher.group(0);
    String integral_lower_index = integralFinalMatcher.group(1);
    String integral_upper_index = integralFinalMatcher.group(2);
    String integral_formula = integralFinalMatcher.group(3);
    String ultimateLatexIntegral = "(\\int_{"+ integral_lower_index
                +"}^{"+ integral_upper_index +"} " + integral_formula + ")";

    mathFormula = mathFormula.replace(integral_user_input, ultimateLatexIntegral);
}

to match these two strings separately, but for now it would interpret it as one. And in result of it I'd get the following latex SVG: Sample picture of wrong regex interpretation

I would like to have output with two separate integrals, like here: Desired regex interpretation How can I achieve this with regex?

Obviously, I seek for an idea that would make it work for more than two pieces.

5
  • 4
    Please use code boxes and not images to represent code and input / output. Commented Oct 30, 2016 at 14:01
  • Discourage screenshots of code and/or errors Commented Oct 30, 2016 at 14:26
  • Use non-greedy quantifiers: (.*?) instead of (.*). Commented Oct 30, 2016 at 16:04
  • Thanks for quick answer! Algorithm works much better, however output is missing one parenthesis. Here's a photo: imgur.com/a/5LdOu .How can I fix it? Commented Oct 30, 2016 at 16:52
  • Fixed it replacing if(integralFinalMatcher.find()) with while(integralFinalMatcher.find()). Using if, it gives only one match istead of all matches found. Commented Oct 30, 2016 at 18:01

1 Answer 1

0

You're doing a lot of work that the Matcher class can do for you. Check it out:

Pattern p = Pattern.compile("integralfrom(?<upper>.*?)to(?<lower>.*?)of(?<formula>.*?)\\)");
Matcher m = p.matcher(subject);
result = m.replaceAll("\\\\int_{${upper}}^{${lower}} (${formula})");

With an input of "integralfrom1to10ofx^2)+integralfrom1to10ofx^3)", the result is:

\int_{1}^{10} (x^2)+\int_{1}^{10} (x^3)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the neat solution! Is there any efficient way to make this regex yet replace partially, I mean when let's say one writes "integralfrom1", above regex snippet replaces it with \int_{1}^{} ()? Or I have to write separate patterns?
Info for Android developers: Named regex groups are not supported by android code base yet, so I had to use one of 3rd-party libs: github.com/tony19/named-regexp#download.
comment 1: You would need to use different regexes or, better still, write a dedicated parser. comment 2: You could also use non-named groups like you were doing originally, and numbered backreferences: "\\\\int_{$1}^{$2} ($3)"
I found now another problem with my regex:stackoverflow.com/questions/40381182/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.