In your pattern, [[a-z]]+
is a malformed bracket expression, because it matches any char from the [[a-z]
bracket expression (any lowercase ASCII letter or [
) and then matches one or more ]
chars. You meant to use single [
and ]
here.
You may use sub
with the following regex:
sub(".*[&?]utm_campaign=([^&]+).*", "\\1", s)
See the regex demo.
Details
.*
- any 0+ chars, as many as possible
[&?]
- a ?
or &
utm_campaign=
- a literal substring
([^&]+)
- Capturing group 1: one or more chars other than &
chars
.*
- any 0+ chars, as many as possible
The \1
is the replacement backreference that puts the contents of Group 1 into the result.
See the R demo:
s <- "xxx/yyy?utm_medium=display&utm_source=ogury&utm_campaign=TOTO&zzz=coco"
sub(".*[&?]utm_campaign=([^&]+).*", "\\1", s)
## => [1] "TOTO"