1

I have a string like the following: 14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27.0

The following regex extracts the last part that ends in a dot and a digit. I want to extract everything but that part and can't seem to find a way to invert the regex (using ^) is not helping:

> s <- '14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27.0'
> str_extract(s, '(\\.[0-9]{1})$')
[1] ".0"

I instead want the output to be:

[1] 14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27

To clarify further, I want it to return the string as is, if it does not end in a dot and one single digit.

Following example:

> s <- '14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27'
> str_extract(s, someRegex)
[1] "14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27"
> s <- '14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27.1'
> str_extract(s, someRegex)
[1] "14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27"
> s <- '14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27.4'
> str_extract(s, someRegex)
[1] "14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27"
Gopala
  • 10,363
  • 7
  • 45
  • 77

4 Answers4

1

Try this regex:

^.*(?=\.\d+$)|^.*

Regex live here.

1

One option would be substituting for the last bit,

sub("\\.\\d$", '', s)
Rorschach
  • 31,301
  • 5
  • 78
  • 129
0
str_extract(s, ([\w ]+(?:\.|\-)){7})

Then you can access the returned string to its lenght-1, and it will give you the required output!

PS: You may have to use escape characters.

XOR-Manik
  • 493
  • 1
  • 4
  • 19
0

You could use stringr::str_remove() for example:

s <- '14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27.0'
stringr::str_remove(s, '(\\.[0-9]{1})$')
#> [1] "14ed0d69fa2.bbd.7f5512.filter-132.21026.55B67C8E27"
Bryan Shalloway
  • 748
  • 7
  • 15