Guillaume
Guillaume

Reputation: 9061

Get a subgroup in regex

In my .htaccess, I do some rewriteURL, but I have some problem to get the data.

I have : RewriteRule ^forum/([0-9]+)-([a-zA-Z]+)/([0-9]+)-([a-zA-Z]+)/?$ index.php?page=forum&category=$1&topic=$3 [NC,L]

So I can get for example : index.php?page=forum&category=123&topic=456 with this URL forum/123-cat/456-topic

But I would like this regex ^forum/([0-9]+)-([a-zA-Z]+)(/([0-9]+)-([a-zA-Z]+))?/?$, but the $3 get 456-topic and not just 456.

So how to get just 456 ?

Thank you>

Upvotes: 1

Views: 133

Answers (1)

Mark Reed
Mark Reed

Reputation: 95315

$3 is the whole thing; the piece you want is $4.

Count by opening parentheses. ([0-9]+) is $1, ([a-zA-Z]+) is $2, (/([0-9]+)-([a-zA-Z]+)) is $3, the ([0-9]+) inside that is $4, and the ([a-zA-Z]+) is $5.

Upvotes: 2

Related Questions