Title: Regular expression with variable in the middle
Last modified: February 28, 2018

---

# Regular expression with variable in the middle

 *  Resolved [webdesigner72](https://wordpress.org/support/users/webdesigner72/)
 * (@webdesigner72)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/regular-expression-with-variable-in-the-middle/)
 * I’m trying to create a regular expression rule for the source url based on the
   following pattern:
 * part1/dynamic/part2/part3
 * I understand how to write a regular expression for when the dynamic part of the
   expression is at the beginning (^/part2/part3) or end (part1/.*), but not in 
   the middle as per above.
 * How could I do this?

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [CEMBTW](https://wordpress.org/support/users/cembtw/)
 * (@cembtw)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/regular-expression-with-variable-in-the-middle/#post-10029395)
 * You want to keep that dynamic part in the destination, or just want it accounted
   for and caught by the regex?
 *  Thread Starter [webdesigner72](https://wordpress.org/support/users/webdesigner72/)
 * (@webdesigner72)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/regular-expression-with-variable-in-the-middle/#post-10035730)
 * The dynamic part will be a username. I am trying to create redirects for some
   Buddypress urls such as:
    members/buddypresstester/settings/notifications/ part1/
   dynamic/part2/part3
 * Since the username part will always be different based on which user’s profile
   is being viewed, I would like to write a redirect rule as a regular expression
   to cater for this dynamic part.
 *  [CEMBTW](https://wordpress.org/support/users/cembtw/)
 * (@cembtw)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/regular-expression-with-variable-in-the-middle/#post-10040961)
 * Regex redirects have capture groups that can be passed to the desired destination.
 * So a structure like: `/oldmembers/myusername/oldposts/` could have a regex like:`
   ^\/oldmembers\/(.*)\/oldposts\/$`.
 * That part in parentheses (now stored as `$1`—subsequent groups would be $2, $
   3, etc.) would be called in your destination like: `/newmembers/$1/newposts/`.
 * Click the ^ arrow next to “Substitute” here to test them out. [https://regex101.com](https://regex101.com)
    -  This reply was modified 8 years, 2 months ago by [CEMBTW](https://wordpress.org/support/users/cembtw/).
    -  This reply was modified 8 years, 2 months ago by [CEMBTW](https://wordpress.org/support/users/cembtw/).
 *  Thread Starter [webdesigner72](https://wordpress.org/support/users/webdesigner72/)
 * (@webdesigner72)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/regular-expression-with-variable-in-the-middle/#post-10059440)
 * Thank you for that, it works almost perfectly. When I click on a link that would
   take me to the redirect path (members/buddypresstester/settings/notifications/)
   I am redirected correctly. But if I copy and paste this address into the address
   bar and hit enter, for some reason the redirect does not happen. Is there a setting
   that can change this so it doesn’t happen?
 * Also, how would I set the target url to the home page or an external url? When
   I type [http://www.google.com](http://www.google.com) into the target url this
   is simply appended onto the address where I currently am. Apologies if these 
   are very basic questions.
    -  This reply was modified 8 years, 2 months ago by [webdesigner72](https://wordpress.org/support/users/webdesigner72/).
 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/regular-expression-with-variable-in-the-middle/#post-10060439)
 * There’s no difference between clicking a link and typing it, so it’s likely that
   the URL you’re typing is different from the one you are clicking.
 * You can just enter the external URL into the target field. You’ll need to make
   sure nothing else is in the target

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Regular expression with variable in the middle’ is closed to new replies.

 * ![](https://ps.w.org/redirection/assets/icon-256x256.jpg?rev=983639)
 * [Redirection](https://wordpress.org/plugins/redirection/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/redirection/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/redirection/)
 * [Active Topics](https://wordpress.org/support/plugin/redirection/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/redirection/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/redirection/reviews/)

 * 5 replies
 * 3 participants
 * Last reply from: [John Godley](https://wordpress.org/support/users/johnny5/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/regular-expression-with-variable-in-the-middle/#post-10060439)
 * Status: resolved