0

I have two regex expressions in R which are giving me the same output, despite the presence of parentheses in one of them. Here they are:

regmatches("[60.0 - 119.0]",regexpr("[0-9]+\\.0\\]$","[60.0 - 119.0]"))
regmatches("[60.0 - 119.0]",regexpr("([0-9]+)\\.0\\]$","[60.0 - 119.0]"))

Both of these expressions yield '119.0]' (without the quotes). It is my understanding that the second expression should find the same piece of the string but only return the piece in parentheses, which in this case is the numbers before the decimal point, like so: '119' (which is what I want).

What am I missing in either my understanding or execution of this code?

cbw
  • 247
  • 3
  • 9
  • `regmatches` only returns the full match. It doesn't do anything with with capture groups. The capture groups are also ignored by `regexpr` unless you use `perl=TRUE`. I wrote a helper function called [regcapturedmatches](https://gist.github.com/MrFlick/10413321) that maybe useful. – MrFlick Nov 09 '17 at 16:10
  • 1
    See also [stringr::str_match](https://rdrr.io/cran/stringr/man/str_match.html) – MrFlick Nov 09 '17 at 16:15
  • Ah. So it's an R mistake, not regex mistake then. Thanks! – cbw Nov 09 '17 at 16:17
  • No, it's a programmer oversight. Neither an R nor a regex mistake. – hrbrmstr Nov 09 '17 at 16:18
  • Right. I meant that in the sense that I made a mistake in R, not in regex. Not blaming either R or regex! – cbw Nov 09 '17 at 16:21

0 Answers0