James Ives
James Ives

Reputation: 3355

Pattern not matching

I’m trying to figure out how to make;

/section/hello-news/goodbye-news

Match when this pattern is used (a single group)

/section/([a-zA-Z0-9\-]+)

But with the way I have it setup only /section/hello-news matches. Where am I going wrong?

Upvotes: 0

Views: 41

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98861

You missed the forward slash /, try this:

/section/([A-Za-z0-9\/-]+)

Regex101 Demo

Upvotes: 1

ReactiveRaven
ReactiveRaven

Reputation: 7541

You are missing the trailing / on hello-news, and allowing it to repeat.

/section(/[a-zA-Z0-9\-]+)+

This way, it matches any number of subdirectories. i.e. '/section/foo/bar/baz'.

If you are using the matches, you can also ignore the slash by making it non-capturing:

/section/(?:/([a-zA-Z0-9\-]+))+

Upvotes: 0

omarjmh
omarjmh

Reputation: 13888

Here is the correct regex, just add another group:

\/section\/([a-zA-Z0-9-]+)\/([a-zA-Z0-9-]+)

https://regex101.com/r/mP0wN2/1

Upvotes: 0

Related Questions