I have a text file with constant numbers. Some of them are double type like 0.0 and the others are float like 0.0f. I'd like to change all numbers into float format which ending with f. But I can find floating number with regular expression but I couldn't distinguish with float type number. Due to that there's a problem like this: 1.0, 1.1, 1.1f, 1.2f, 2.0 -> 1.0f, 1.1f, 1.1ff, 1.2ff, 2.0f

I'd like to change these samples like this: 1.0f, 1.1f, 1.1f, 1.2f, 2.0f

My regulation expression to find floating number is like this: Pattern: ([-+]?)([0-9]+)(.{1})([0-9]+)([eE]?[+-]?[0-9]+)? Replace: $1$2$3$4f

Is anybody who can distinguish from float and double numbers?

Thanks in advance.

有帮助吗?

解决方案

Use a Negative lookahead (see syntax)

For example:

((?:[-+]?)(?:[0-9]+)(?:.{1})(?:[0-9]+)(?:[eE]?[+-]?[0-9]+)?)(?!f)

And your replace pattern is

$1f

Note that I've replaced your capture groups with passive groups and added a single group for the number

EDIT: Alternative (have only tested with your test string...)

((?:[-+]?)(?:[0-9]+)(?:.{1})(?:[0-9]+)(?:[eE]?[+-]?[0-9]+)?)([^f]?,|$)

replacing with

$1f$2
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top