Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:53:59+00:00 2026-05-30T02:53:59+00:00

I would like to match url pattern that has optional segments. I have URL-s

  • 0

I would like to match url pattern that has optional segments.

I have URL-s like this:

subdomain.domain.com/page/pageurl/pagename/123/
subdomain.domain.com/page/pageurl/pagename/
subdomain.domain.com/page/pageurl/
subdomain.domain.com/page/

Now I have a regex that matches all those situations:

^([a-z]+)\.domain\.com\/page(\/[a-z]+)?(\/[a-z]+)?(\/[0-9]+)?\/?$

But this regexs fails if you go to this URL:

subdomain.domain.com/page/123/

It matches this url too, and I dont want that to happen beacuse first segment should be [a-z]+ and nothing else. Now I do understand why is this happening, but I cant figure out the right regexs to suite my needs.
I need a regexs that would match those URL-s but in order, so if first segment after page is number, it should not match…

How would I do that? Im going crazy right now :S

Rubural example: LINK

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:54:01+00:00Added an answer on May 30, 2026 at 2:54 am

    We can make the capturing group of the first ‘segment’ mandatory and all of the segments optional like so:
    ^([a-z]+)\.domain\.com\/page(?:(\/[a-z]+)(\/[a-z]+)?(\/[0-9]+)?)?\/?$

    Another thing that might be useful is to allow any valid subdomain, the pattern would look like this:

    ^([\w.-]+)+\.domain\.com\/page(?:(\/[a-z]+)(\/[a-z]+)?(\/[0-9]+)?)?\/?$

    Edit: Fixed pattern, as Umbrella pointed out (thanks) my prevous pattern would not match your last example string, oops

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a route like following, ideally I would like it to match: domain.com/layout/1-slug-is-the-name-of-the-page
What I need is something like this: /<[\w\d]+ ([\w\d]+\=[w\d])+\/>/ Something that would match several
I'd like to specify a Servlet URL pattern to match a URL that ends
In my Django application, I have a URL I would like to match which
I have a problem with regular expressions. I would like to match strings that
I would like to use the API to return all tweets that match my
I would like to use Apache RewriteRule to change the URL target page to
I have a Servlet that sends back a JSON Object and I would like
If say I have a route that is defined like this: {books}/{*all} , then
I'd like to find a Firefox addon that would look at the url you're

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.