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: 
If you want to return the string without the last `/` you can do this several ways. Below are a couple options using base R:

Using a back-reference in `gsub()` (`sub()` would work too here):

```
gsub("(.*?)/*$", "\\1", x)
[1] "http://compras.dados.gov.br/materiais/v1/materiais.html?pdm=08275"

# or, adapting your original pattern
gsub("((http:////)?compras\\.dados\\.gov\\.br.*\\?.*?)/*$", "\\1", x)
[1] "http://compras.dados.gov.br/materiais/v1/materiais.html?pdm=08275"

```

By position using `ifelse()` and `substr()` (this will proabbly be a little bit faster if scaling matters)

```
ifelse(substr(x, nchar(x), nchar(x)) == "/", substr(x, 1, nchar(x)-1), x)
[1] "http://compras.dados.gov.br/materiais/v1/materiais.html?pdm=08275"

```

**Data**:

```
x <- "http://compras.dados.gov.br/materiais/v1/materiais.html?pdm=08275/"

```
How about trying `gsub("(.*?)/+$","\\1",s)`?