Hi, @scott-sumner and All,
Ah, yes, Scott, using named capturing groups is a solution for documented regexes. But there a nice other way to get correct regexes, with a lot of comments !
I tried to rewrite your S/R, with named groups, using the following template :
SEARCH :
(?x) (?-i) # The search is NON-insensitive ( => Sensitive ! ) \[ # A single opening square bracket ( ESCAPED as special char. ) ( # Beginning of group 1 ( First Name ) [A-Z] # A single capital letter [a-z]+ # A NON-null range of lower-case letters ) # End of group 1 , # A single comma character ( # Beginning of group 2 ( FL separator ) \x20|\r\n # A single space character OR the TWO Window End of Line characters ) # End of group 2 ( # Beginning of group 3 ( Last Name ) [A-Z] # A single capital letter [a-z]+ # A NON-null range of lower-case letters ) # End of group 3 (?: # Beginning of an OPTIONAL, non-capturing, group ( # Beginning of group 4 ( MI separator ) \x20|\r\n # A single space character OR the TWO Window End of Line characters ) # End of group 4 ( # Beginning of group 5 ( Middle Initial ) [A-Z] # A single capital letter ) # End of group 5 \. # A single dot character ( ESCAPED as special char. ) )? # End of the OPTIONAL group 5 \] # A single ending square bracket ( ESCAPED as special char. )Unfortunately, this way of writing does NOT work in the replacement part :
# The replacement part CANNOT be split in SEVERAL lines !! # # \3, # Last name is written first, followed by a comma # \2 # Then, we add the FL separator # \1 # Then, the First name is written # ?5 # And if group 5 ( Middle Initial ) exists : # \4\5 # We rewrite group 4 ( MI separator ), followed by group 5 ( Middle Initial )=> REPLACEMENT :
\3,\2\1?5\4\5Now :
Select all the lines of the SEARCH part, above, between (?x) and \]
Copy them, in the clipboard, with a Ctrl + C shortcut
Paste, first, this selection, in your current file, with a Ctrl + V shortcut
Re-select this text, representing the search part
Open the Replace dialog ( Ctrl + H )
Paste the correct replacement regex, above, in the Replace with: zone
Select the Regular expresion search mode
Click on the Replace All button
Et voilà !!
Notes :
Once the search part selected, DON’T copy this selection in the clipboard, for further pasting, in the Find what: zone, of the Replace dialog ! Simply, open the Replace dialog :-) : The selection will be filled in the Find what: zone, automatically :-)
The syntax (?x) syntax MUST begin the subsequent lines, of the regex. This modifier starts a free-spacing and comment way of writing regexes, with a # character, beginning the comment part
As, in this mode, the space character is simply ignored, if you search for a space character, you’ll have to use one of the three following syntaxes : \ , [ ] or \x20
Best Regards,
guy038