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.

Xai Nano
  • 89
  • 3
  • 9
  • 4
    Please use code boxes and not images to represent code and input / output. – Ronen Ness Oct 30 '16 at 14:01
  • [Discourage screenshots of code and/or errors](http://meta.stackoverflow.com/q/303812/995714) – phuclv Oct 30 '16 at 14:26
  • Use non-greedy quantifiers: `(.*?)` instead of `(.*)`. – Alan Moore Oct 30 '16 at 16:04
  • Thanks for quick answer! Algorithm works much better, however output is missing one parenthesis. Here's a photo: http://imgur.com/a/5LdOu .How can I fix it? – Xai Nano Oct 30 '16 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. – Xai Nano Oct 30 '16 at 18:01

1 Answers1

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)
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • 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? – Xai Nano Oct 31 '16 at 12:05
  • 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: https://github.com/tony19/named-regexp#download. – Xai Nano Oct 31 '16 at 12:12
  • **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)"` – Alan Moore Oct 31 '16 at 17:20
  • I found now another problem with my regex:http://stackoverflow.com/questions/40381182/java-regex-ability-to-handle-nested-matches-separately – Xai Nano Nov 02 '16 at 18:36