0

I want to check if a string has either this format "1234.34" or this "1234". I tried the following using grepl in R (1 or more numbers, a dot or not followed by more numbers):

grepl("([[:digit:]]+\\.?[[:digit:]]*)", c("1234.34", "1234,34", "1234"))
# [1] TRUE TRUE TRUE

With 1234.34 or 1234 it works fine (both TRUE).

If I test "1234,34" (with "comma"), the expression becomes TRUE, but I expected FALSE.

zx8754
  • 52,746
  • 12
  • 114
  • 209
arnyeinstein
  • 669
  • 1
  • 5
  • 14
  • Related: https://stackoverflow.com/questions/4105956/regex-does-not-contain-certain-characters – zx8754 Mar 23 '23 at 11:43

2 Answers2

2

You could just include both options in the regex pattern: either only digits or digits + dot + more digits. Separate both options with a pipe |:

x <- c("1234.34", "1234,34", "1234")
grepl("^\\d+$|^\\d+\\.\\d+$", x)
#> [1]  TRUE FALSE  TRUE

Created on 2023-03-23 with reprex v2.0.2

dufei
  • 2,166
  • 1
  • 7
  • 18
1

As long as the string does only contain numbers, you could do the following that searches for anything that's not a comma:

x <- c("1234", "1234.34", "1234,34")
grepl("^[^,]+$", x)

[1]  TRUE TRUE FALSE
Julian
  • 6,586
  • 2
  • 9
  • 33