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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:55:23+00:00 2026-05-30T13:55:23+00:00

Hello I have written a regular expression for swapping a name and sourname: find

  • 0

Hello I have written a regular expression for swapping a name and sourname:

find \"([A-Z][a-z]+)\s([A-Z][a-z]+)\" replace "\2 \1"

However I have now discovered that I should not swap everything I find inside brackets but only particoular candidate names such as:

  • John Doe
  • Jayne Eyre

For example if I find Jayne Doe it should not be swapped (so \”(Eyre|Doe)\s(John|Jayne)\” is excluded from the possible solutions).

Any advice. 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-30T13:55:25+00:00Added an answer on May 30, 2026 at 1:55 pm

    Assuming the number of names to be switched is fairly small, this might work for you:

    (?: (?<A>John)\s(?<B>Doe) | (?<A>Jayne)\s(?<B>Eyre) | (?<B>\w+)\s(?<A>\w+) )
    

    I have included white space for readability; be sure to take it out or use the regex option that ignores it. I used Expresso, so the regexes are in the .Net syntax, but I think you can tweak it as needed.

    The replacement regex is:

    ${B} ${A}
    

    The regex has 3 alternatives; 2 (or more) for the names that we want to switch, and one for all other names that should not be switched. The name parts are defined with named capture groups (A & B) that are used by the replacement regex to do the switching. The trick is just that in the third alternative, we swap the named capture groups so that names that we do not want to switch are actually switched, but to no effect.

    More complicated name variations (John C. Reilly, Olivia Newton-John) will not work, but as your original regex kept it simple, I followed suit.

    Updated answer……

    It seems Flex regexes do not support named groups in the replacement regex. After experimenting, I have come up with a variation of my original idea that only uses numbered capture groups and seems to work:

    (John)\s(Doe)|(Jayne)\s(Eyre)|(\w+)\s(\w+)
    

    The replacement regex is:

    $2$4$5 $1$3$6
    

    This is quite a bit more of a hack than the original, but again, if the number of names to be switched is reasonable, it might be workable. In my testing, given above regex and replacement regex and the following input:

    John Doe
    Jayne Eyre
    Bob Smith
    

    I got the following output:

    Doe John
    Eyre Jayne
    Bob Smith
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have been having trouble with this for a while now. I have
Hello I have this list that i am working on. When i move the
Hello there i have written some code in jquery to add new input type
I have written a Hello, world program with JNI. Java calls c program with
I have written a simple Hello World program. #include <stdio.h> int main() { printf(Hello
I have written a command-line tool that uses sub-commands much like Mercurial, Git, Subversion
I have written this code that loops through an array of lat/long coordinates and
I have written a simple application that displays a list of candidates for a
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
I have written a printf() statement like below: printf(hello\n); this works fine when built

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.