James
James

Reputation: 229

Possible to capture part of one capture group into a second capture group?

Example:

blah blah href='http://www.domain.com/keyid=432' blah blah blah blah

So if i use a regex like

href='(.*)' 

that captures the url into group 1 but is it possible to also capture the keyid "432" into a second group? Im sure there is a way to achieve this but i am still a regex noob.

Upvotes: 3

Views: 134

Answers (2)

tchrist
tchrist

Reputation: 80443

Yes, capture groups can contain other capture groups.

But you really have to be far more careful with using regexes on HTML than very nearlhy anybody ever is. Here’s one pair of approaches, and here’s another.

I find that few programmers are ever even as moderately careful as the most naïve of those three solutions is, let alone as cautious as the deep wizardry that the other two are engaged in.

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 839114

Yes, you can nest capturing groups:

href='(.*/keyid=([0-9]+))' 

Upvotes: 3

Related Questions