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?
Assuming the number of names to be switched is fairly small, this might work for you:
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:
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:
The replacement regex is:
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:
I got the following output: