We will break down how to write a regex pattern that matches numbers, including integers and floating-point numbers, with optional signs (+
or -
). Then, we’ll provide multiple examples to show how it works in different scenarios.
[+-]?
: Match an optional +
or -
sign.\d+
: Match one or more digits.(\.\d+)?
: Match an optional decimal point followed by one or more digits (for floating-point numbers).