1

I want to check if a character can be safely converted to a numeric by using a regex.

However, I don't see my error. Example:

stringr::str_detect("4.", pattern = "-{0,1}[0-9]+(.[0-9]+){0,1}")

This produces a TRUE. My intention was to specifiy that whenever a . follows the first sequence of numbers, there must be at least one other number, therefore (.[0-9]+){0,1}.

What's wrong here?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Ai4l2s
  • 525
  • 2
  • 9

1 Answers1

0

Note:

  • (.[0-9]+){0,1} is an optional pattern because {0,1} (=?) makes the .[0-9]+ pattern sequence match one or zero times. So, yes, one or more digits ([0-9]+) must follow any char other than line break chars (matched with an unescaped .), but this pattern is optional, and thus you cannot require anything with it.
  • . is unescaped, so it matches any char other than line break chars. Escape it to match a literal dot
  • Your regex is not anchored, and can match partial substrings in a longer string. Use ^ and $ to make the pattern match the whole string.

So, consider using

stringr::str_detect("4.", pattern = "^-?[0-9]+(?:\\.[0-9]+)?$")

where

  • ^ - start of string
  • -? - an optional - char
  • [0-9]+ - one or more digits
  • (?:\.[0-9]+)? - a non-capturing group matching an optional sequence of a . and then one or more digits
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563