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

  • Home
  • SEARCH
  • 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 3497274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:21:02+00:00 2026-05-18T12:21:02+00:00

For example: I know how to match www.domain.com/foo/21 sub foo : Path(‘/foo’) Args(1) {

  • 0

For example:

I know how to match http://www.domain.com/foo/21

sub foo : Path('/foo') Args(1) {
  my ( $self, $c, $foo_id ) = @_;
  # do stuff with foo
}

But how can I match http://www.domain.com/foo/21 OR http://www.domain.com/foo/21/bar/56 ?

sub foo : <?> {
  my ( $self, $c, $foo_id, $bar_id ) = @_;
  # do stuff with foo, and maybe do some things with bar if present
}

Thanks

Update:
Following Daxim’s suggestion, I tried to use :Regex

sub foo : Regex('foo/(.+?)(?:/bar/(.+))?') {
   my ( $self, $c ) = @_;
   my ( $foo_id, $bar_id ) = @{ $c->req->captures };
}

But this doesn’t seem to work; the url is matched, but $bar_id is always undef. If I remove the optional opperator from the end of the regex then it does capture $bar_id correctly, but then both foo and bar must be present to get a url match. I’m not sure if this is a perl regex issue, or a Catalyst issue. Any ideas?

Update:

As Daxim points out, its a regex issue. I can’t see why the above regex doesn’t work, but I did manage to find one that does:

sub foo : Regex('foo/([^/]+)(?:/bar/([^/]+))?') {
   my ( $self, $c ) = @_;
   my ( $foo_id, $bar_id ) = @{ $c->req->captures };
}

(I didn’t use \d+ in the captures like Daxim did as my ids might not be numeric)

Thanks all for the help and suggestions, I learnt a lot about handling urls in Catalyst 😀

  • 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-18T12:21:03+00:00Added an answer on May 18, 2026 at 12:21 pm

    See item Pattern-match (:Regex and :LocalRegex) in Catalyst::Manual::Intro#Action_types.


    nick writes:

    I’m not sure if this is a perl regex issue, or a Catalyst issue. Any ideas?

    How about simply trying it out?

    repl>>> $_ = '/foo/21/bar/56'
    /foo/21/bar/56
    
    repl>>> m|foo/(\d+)(?:/bar/(\d+))?|
    $VAR1 = 21;
    $VAR2 = 56;
    
    repl>>> $_ = '/foo/21'
    /foo/21
    
    repl>>> m|foo/(\d+)(?:/bar/(\d+))?|
    $VAR1 = 21;
    $VAR2 = undef;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to retrieve text from a remote web site that does not provide
I want to replace every link I find within a string, with a modified
Possible Duplicate: How to validate an emailaddress in PHP I was wondering what everyone
I am trying to parse somewhat standard XML documents that use a schema called
Ok, I'm relatively new to rails and am working through the book Rails for
I have a table called People with a columns of datatype xml called properties
I have this array with 3 main keys. Images, Downloads and Videos. Here's a
I'd like to understand better what is fail-fast and fail-safe. What it seems to
UPDATE - New code at the bottom I'm trying to figure out how to

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.