Home > database >  Regular expression for pattern XX.XX.XX. where X is any number 0-9?
Regular expression for pattern XX.XX.XX. where X is any number 0-9?

Time:06-09

This is probably a simple question but I don't have much experience with regular expressions and I can't figure it out on my own. I have tried putting together the expression based on cheat sheets but none have come close to working apart from this one [0-9.0-9\\.0-9.0-9\\.0-9.0-9\\.], but this one also let through a wrong string.

Basically I want to check if all rows in certain columns follow that pattern exactly (that is, there are no other characters) so I can correct those cells which don't and then do further manipulations. Some examples of matching strings would be 01.12.20. or 26.02.11., and some that don't match would be 01.12.20, 26.0.2.11. etc.

Thank you!

CodePudding user response:

A more economical regex, that will also catch entries with incorrect leading or trailing entries would be

grep("^(\\d{2}\\.){3}$", str)

To find the entries that don't match the pattern (as described in the question) we could do:

str <- c("01.12.20.",  "26.02.11.", "01.12.20,", "26.0.2.11.")

grep("^(\\d{2}\\.){3}$", str, invert = TRUE)
#> [1] 3 4

Created on 2022-06-08 by the reprex package (v2.0.1)

CodePudding user response:

I believe this is the pattern that you want:

[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}\\.

CodePudding user response:

To be completle sure you can use the following RegEx:

^(\d{2}\.){3}$
  • Related