pankaj
pankaj

Reputation: 11

Extract a part of string on a particular reference

I need to extract number that comes after "&r=" in the below link.

http://asdf.com/product/eyewear/eyeglasses?Brand[]=Allen%20Solly&r=472020&ck-source=google-adwords&ck-campaign=eyeglasses-cat-brand-broad&ck-adgroup=eyeglasses-dersdc-cat-brand-broad&keyword={keyword}&matchtype={matchtype}&network={network}&creative={creative}&adposition={adposition}

Here's what i tried C has my link stored in.

sub(".*&r=", "",c)

"472020&ck-source=google-adwords&ck-campaign=eyeglasses-cat-brand-broad&ck-adgroup=eyeglasses-dersdc-cat-brand-broad&keyword={keyword}&matchtype={matchtype}&network={network}&creative={creative}&adposition={adposition}"

This only gives me whole after part of the string .

I only need the number i.e 472020 .

Any idea?

Upvotes: 1

Views: 57

Answers (3)

akrun
akrun

Reputation: 887601

We can use str_extract

library(stringr)
as.numeric(str_extract(z, "(?<=\\&r\\=)\\d+"))
#[1] 472020

If there are several matches use str_extract_all in place of str_extract

Upvotes: 1

plannapus
plannapus

Reputation: 18759

For completeness sake, here it is with the base R regmatches/regexpr combo:

 regmatches(z, regexpr("(?<=\\&r\\=)\\d+",z,perl=TRUE))

It uses the same Perl-flavoured regex as @akrun's stringr version. regexpr (or gregexpr if several matches of the same pattern are expected in the same string) matches the pattern, while regmatches extracts it (it is vectorized so several strings can be matched/extracted at once).

> as.integer(regmatches(z,regexpr("(?<=\\&r\\=)\\d+",z,perl=TRUE)))
#[1] 472020

Upvotes: 1

Sotos
Sotos

Reputation: 51592

Here is how to get it using sub

sub(".*=(\\d+)&.*", "\\1", z)
#[1] "472020"

or

as.integer(sub(".*=(\\d+)&.*", "\\1", z))
#[1] 472020

Upvotes: 1

Related Questions