0

I am learning regex and ran into a problem. I have the following:

href="http://google.com/topic/713725-dogs-are-great-3"> href="http://google.com/topic/213225-humans-are-great"> href="http://google.com/topic/342315-cats-are-great-but-small">

with this code href="(?:[^"]*)topic/([^<]*)">

i can select

713725-dogs-are-great-3 213225-humans-are-great 342315-cats-are-great-but-small

but i only want to match the numbers

342315 213225 713725

any ideas?

  • you could find `/topic/(\d{6})-` .. but using an intermediate XML parser will help you out! see https://stackoverflow.com/a/1732454/4541045 – ti7 Aug 27 '21 at 15:47

1 Answers1

3

With your shown samples and attempts, please try following regex; which will create 1 capturing group which you can use to grab the matched value.

\bhref="(?:[^"]*)topic\/(\d+)-.*">$

Online demo for above regex

Explanation: Adding detailed explanation for above.

\bhref="          ##Matching href using \b before it as a word boundary followed by =" here.
(?:[^"]*)topic\/  ##In a non-capturing group matching till " everything till topic/
(\d+)             ##Creating 1st capturing group which has digits in it.
-.*">$            ##Matching everything till " followed by "> till end of value.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93