Question: Given the string `"http://compras.dados.gov.br/materiais/v1/materiais.html?pdm=08275/"`, I need to generate a regex filter so that it ignores the last char if it is an "/" .

I tried the following regex `"(http:////)?compras\\.dados\\.gov\\.br.*\\?.*(?<!//)"` as of [regexr.com/4om61](http://regexr.com/4om61), but it doesn´t work when I run in R as:

```
regex_exp_R   <- "(http:////)?compras\\.dados\\.gov\\.br.*\\?.*(?<!//)"
grep(regex_exp_R, "http://compras.dados.gov.br/materiais/v1/materiais.html?pdm=08275/", perl = T, value = T)

```

I need this to work in pure regex and grep function, without using any string R package.
Thank you.

**Simplified Case:**
After important contributions of you all, one last issue remains. 
Because I will use regex as an input in another friunction, the solution ***must work with pure regex and grep***.

The remaining point is a very basic one: given the strings "a1bc/" or "a1bc", the regex must return "a1bc". Building on suggestions I received, I tried

`grep(".*[^//]" ,"a1bc/", perl = T, value = T)`, but still get "a1bc/" instead of "a1bc". Any hints? Thank you.

Answer: 
Use `sub` to remove a trailing `/`:

```
x <- c("a1bc/", "a2bc")
sub("/$", "", x)

```

This changes nothing on a string that does not end in `/`.

As others have pointed out, `grep` does not modify strings. It returns a numeric vector of indices of the matched strings or a vector of the (unmodified) matched items. It's usually used to subset a character vector.
How about trying `gsub("(.*?)/+$","\\1",s)`?