I'm trying to find whether a string pattern; (<double num><space><an operator><space><double num>)
e.g. (14.0 + 46.0) exists in a given string using a regex in R. There can be 4 operators +,-,* and /.
There are two main patterns. Regex for the 1st pattern identifies that the pattern exists in the string "s"
#Pattern 1
s = "(14.0 + 46.0)"
#Regex
grep("^\\(-?\\d*\\.\\d{1}\\s[\\+\\-\\*\\/]\\s-?\\d*\\.\\d{1}\\)$", s)
I'm trying to find the same pattern in a different string s1 and s2. I modified the first regex by adding .*
(any character) to the beginning and end of the string ("^.* .*$")
. I have checked the regex in this online checker and it works. But it doesn't work in R studio.
#Pattern 2
s1 = "((5.0 - 50.0) - 15.0)"
s2 = "(15.0 - (5.0 - 50.0))"
#Regex
grep("^.*\\(-?\\d*\\.\\d{1}\\s[\\+\\-\\*\\/]\\s-?\\d*\\.\\d{1}\\).*$", s1)