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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:10:41+00:00 2026-06-09T05:10:41+00:00

I’m basically not in the clue about regex but I need a regex statement

  • 0

I’m basically not in the clue about regex but I need a regex statement that will recognise anything after the / in a URL.

Basically, i’m developing a site for someone and a page’s URL (Local URL of Course) is say (http://)localhost/sweettemptations/available-sweets. This page is filled with custom post types (It’s a WordPress site) which have the URL of (http://)localhost/sweettemptations/sweets/sweet-name.

What I want to do is redirect the URL (http://)localhost/sweettemptations/sweets back to (http://)localhost/sweettemptations/available-sweets which is easy to do, but I also need to redirect any type of sweet back to (http://)localhost/sweettemptations/available-sweets. So say I need to redirect (http://)localhost/sweettemptations/sweets/* back to (http://)localhost/sweettemptations/available-sweets.

If anyone could help by telling me how to write a proper regex statement to match everything after sweets/ in the URL, it would be hugely appreciated.

  • 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-06-09T05:10:43+00:00Added an answer on June 9, 2026 at 5:10 am

    To do what you ask you need to use groups. In regular expression groups allow you to isolate parts of the whole match.

    for example:

    input string of: aaaaaaaabbbbcccc 
    
    regex: a*(b*) 
    

    The parenthesis mark a group in this case it will be group 1 since it is the first in the pattern.

    Note: group 0 is implicit and is the complete match.

    So the matches in my above case will be:

    group 0: aaaaaaaabbbb
    group 1: bbbb
    

    In order to achieve what you want with the sweets pattern above, you just need to put a group around the end.

    possible solution: /sweets/(.*)
    

    the more precise you are with the pattern before the group the less likely you will have a possible false positive.

    If what you really want is to match anything after the last / you can take another approach:

     possible other solution: /([^/]*)
    

    The pattern above will find a / with a string of characters that are NOT another / and keep it in group 1. Issue here is that you could match things that do not have sweets in the URL.

    Note if you do not mind the / at the beginning then just remove the ( and ) and you do not have to worry about groups.

    I like to use http://regexpal.com/ to test my regex.. It will mark in different colors the different matches.

    Hope this helps.

    I may have misunderstood you requirement in my original post.

    if you just want to change any string that matches
    (http://)localhost/sweettemptations/sweets/*

    into the other one you provided (without adding the part match by your * at the end) I would use a regular expression to match the pattern in the URL but them just blind replace the whole string with the desired one:
    (http://)localhost/sweettemptations/available-sweets

    So if you want the URL:

      http://localhost/sweettemptations/sweets/somethingmore.html
    

    to turn into:

      http://localhost/sweettemptations/available-sweets
    

    and not into:

      localhost/sweettemptations/available-sweets/somethingmore.html
    

    Then the solution is simpler, no groups required :).

    when doing this I would make sure you do not match the “localhost” part. Also I am assuming the (http://) really means an optional http:// in front as (http://) is not a valid protocol prefix.

    so if that is what you want then this should match the pattern:

    (http://)?[^/]+/sweettemptations/sweets/.*
    

    This regular expression will match the http:// part optionally with a host (be it localhost, an IP or the host name). You could omit the .* at the end if you want.

    If that pattern matches just replace the whole URL with the one you want to redirect to.

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.