remi
remi

Reputation: 779

Pattern matching to extract string matching conditions

I am trying to extract a string matching a pattern in a string. To make sense:

 x <- "this.is.fairly//Whatit.is/path/IDbeginUntilhere7/seenit"

The objective is of the regex is to return: IDbeginUntilhere. I tried this:

 str <- regmatches(x, gregexpr("^I.*7$", x))

which I understand it doesn't work since the I is located in the middle of the string. The question may be too simple, but I'd appreciate any help I can get.

Upvotes: 2

Views: 97

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627488

It is clear the main issue is the anchors: start of string ^ and end of string $.

The secondary issue is the greedy dot that will also match across / delimited subparts (i.e. will match the whole Id7/Not-to-match7 instead of Id7).

You need to use something like

str <- regmatches(x, gregexpr("I[^/]*7", x))

See regex demo

If you do not need the 7, you need to use a look-ahead, and a Perl-like regex:

str <- regmatches(x, gregexpr("I[^/]*(?=7)", x, perl=TRUE))

See another demo

Upvotes: 2

Related Questions