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 6768927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:07:56+00:00 2026-05-26T15:07:56+00:00

I try to parse url with a regular expression in order to capture element,

  • 0

I try to parse url with a regular expression in order to capture element, but i don’t know how to do. Samples of URL :

  • location-cottage with $path_ => array( type => cottage )
  • location-cottage-p1 with $path_ => array( type => cottage, page => p1 )
  • location-cottage-my-region-r01 with $path_ => array( type => cottage, region => r01 )
  • location-cottage-my-department-d01 with $path_ => array( type => cottage, department => d01)
  • location-cottage-my-department-d01-p1 with $path_ => array( type => cottage, department => d01, page => p1)

I would like to do this with one regular expression, but i don’t know to do this, i try with this :

$expression = '#location-(?P<type>cottage|house)[a-z,-]*';
$expression.= '(?P<region>r[0-9]{2}|)';
$expression.= '(?P<department>d[0-9]{2}\)';
$expression.= '(?P<town>v[0-9]{5}|)';
$expression.= '[-]*(?P<page>[p0-9]*)$#';
preg_match($expression, $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], $path_);

Someone can help me ?

In a second part, if it’s possible I would like to keep only 01 and not d01, only 1 and not p1, as this :

  • location-cottage-my-department-d01-p1 with $path_ => array( type => cottage, department => 01, page => 1)
  • 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-26T15:07:57+00:00Added an answer on May 26, 2026 at 3:07 pm

    Firstly, use #x to make your regex more readable. Then use ? behind each capture group that can be optional:

    $expression = <<< RX 
        #
          location-(?P<type>cottage|house)[a-z,-]*
          (?P<region> r[0-9]{2}|)   ?
          (?P<department> d[0-9]{2})   ?
          (?P<town> v[0-9]{5}|)   ?
          [-]*(?P<page> [p0-9]*)   ?
        $#x
    RX;
    

    And if you don’t want to capture the d for example, then move it out of the named capture group, and wrap it in (?: )? instead.

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

Sidebar

Related Questions

I'm try to parse this xml, but c# keeps throwing an exception saying it
I try to parse RSS chaanal with simple-rss lib. Unfortunately I got a lot
Is there a way to tell ifort or gfortran to just try to parse
I'm trying to parse a webpage using Java with URLConnection. I try to set
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
i try to parse within my iPhone SDK 4 http://de.news.search.yahoo.com/news/rss?p=iphone&ei=UTF-8&fl=0&x=wrt there are some german
this question might sound a little bit weired... But I will try to explain:
i want to parse 1 url and afterwords i want to access some data
When I try to retrieve information from Google weather API with the following URL,
I have an xml feed at this url Now im trying parse the content,

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.