Gopala
Gopala

Reputation: 10483

stringr package str_extract() with inversion of the regex

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"

Upvotes: 1

Views: 749

Answers (4)

Bryan Shalloway
Bryan Shalloway

Reputation: 898

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"

Upvotes: 0

Rorschach
Rorschach

Reputation: 32426

One option would be substituting for the last bit,

sub("\\.\\d$", '', s)

Upvotes: 1

XOR-Manik
XOR-Manik

Reputation: 503

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.

Upvotes: 0

user4227915
user4227915

Reputation:

Try this regex:

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

Regex live here.

Upvotes: 1

Related Questions