3

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.

James
  • 229
  • 2
  • 13

2 Answers2

3

Yes, you can nest capturing groups:

href='(.*/keyid=([0-9]+))' 
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

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.

Community
  • 1
  • 1
tchrist
  • 78,834
  • 30
  • 123
  • 180