2

I have a string with a math formula like

5+10/3-3.0 *(4+8.0)

and I want to replace all int values to double

Result:

5.0+10.0/3.0-3.0 *(4.0+8.0)

I can find the double values with ([\d]*)\.([\d]*) but I want to find all ([\d]*) that are not a part of a double value

RCaetano
  • 642
  • 1
  • 8
  • 23
Alex
  • 97
  • 9
  • Try [`(?<!\.)\b\d+\b(?!\.\d)`](https://regex101.com/r/kG0ta8/1) – Wiktor Stribiżew Oct 07 '16 at 09:22
  • I may have something, but it only works in combination with a callback function, which makes it language-dependent. What are you using it with? – coladict Oct 07 '16 at 09:33
  • 2
    Note that the regex from [`Regular Expression for whole numbers and integers?`](http://stackoverflow.com/questions/16774064/regular-expression-for-whole-numbers-and-integers) won't help if OP has `5-10/3-3.0 *(4+8.0)` as input. – Wiktor Stribiżew Oct 07 '16 at 09:37
  • [*Questions may be duplicates if they have the same (potential) answers... If it's only a matter of changing some numerical values or some variable names, **they're duplicates.***](http://meta.stackexchange.com/a/10844/335649). And I'd like to bring your attention to *Don't answer questions that have already been answered elsewhere. Yeah, you might earn a couple of points of reputation, but, because you are duplicating content, you are actually making the internet worse.* Is it a matter of removing `-`? It's a duplicate! @WiktorStribiżew – revo Oct 07 '16 at 15:33

2 Answers2

2

You can use backreference for that. All integers should consist from digits and should not precede by dot and succeed by dot or another digit (because we would rather capture all digits symbol together). Example for C# flavour:

(?<!\.)(\d+)(?![\.\d])
lerthe61
  • 370
  • 1
  • 16
  • This regex is compatible with more than .NET flavor. But still I'd advise a word boundary at the beginning. Or adding a `\d` to the lookbehind, too, or digits will be matched in `a23` (possibly not in this scenario, but can be helpful in other cases). – Wiktor Stribiżew Oct 07 '16 at 10:39
0

Since we don't know what language you're doing it on, and how to handle it, the only portable solution for mass replacement is to do it twice. First run one with \b(\d+)\b replacing group 1 with 1 + ".0", then run a second time with \.0\. and replace it all (group 0) with a single dot. While this solution is heavier, it's the simplest and most universal.

The less-portable expression to use is ((\d+)(\.\d+)?) with conditional processing. Here's a Java example for it:

java.util.regex.Pattern pc = java.util.regex.Pattern.compile("((\\d+)(\\.\\d+)?)");
java.util.regex.Matcher m = pc.matcher("5+10/3-3.0 *(4+8.0)");
StringBuffer sb = new StringBuffer();
while (m.find()) {
    m.appendReplacement(sb, m.group(3) == null ? m.group(1) + ".0" : m.group(1));
}
m.appendTail(sb);
System.out.println(sb.toString());

It is also possible to use this expression in PHP with a callback function to do the same.

coladict
  • 4,799
  • 1
  • 16
  • 27