Home > front end >  wrap phrases in quotes Notepad
wrap phrases in quotes Notepad

Time:12-11

I've looked a fair amount and can't find an exact solution to this. I need to wrap phrases in quotes in Notepad . My source looks similar to below

Header Alpha, Header Month, Header Year, Header Beta,
        Header Gamma, Header Delta, Header 
Epsilon, Header Zeta, Header Eta Theta, Header Lambda

As you can see some of the phrases have 2 words, some have 3. Some are on one line, some split between one line and the next. I've got something that sort of works where my regex code is

([ ]|[])([A-Za-z0-9 ] )(,)|([A-Za-z0-9 ] \r\n[A-Za-z0-9] )(,)

in the find statement.

  1. ([ ]|[]) = match space or no space
  2. ([A-Za-z0-9 ] )(,)|([A-Za-z0-9 ] \r\n[A-Za-z0-9] ) = match character or space until comma OR match character or space until carriage return line feed then match character or space until comma

The find function works but it's clumsy and the replace function breaks because sometimes I'm wanting to replace with \2 and sometimes I'm wanting to replace with \3.

Seeking advice on getting this all organized like

"Header Alpha",
"Header Month",
"Header Year",
"Header Beta",
"Header Gamma",
"Header Delta",
"Header Epsilon",
"Header Zeta",
"Header Eta Theta",
"Header Lambda",

CodePudding user response:

Here is an idea how to achieve the expected results for phrases of one, two or three words:

Find What: \s*(\w )(?:\s (\w ))?(?:\s (\w ))?(,)?
Replace With: "$1(?2 $2)(?3 $3)"(?4,\n)

Details:

  • \s* - zero or more whitespaces
  • (\w ) - Group 1: one or more word chars
  • (?:\s (\w ))? - an optional sequence of one or more whitespaces and then one or more word chars captured into Group 2
  • (?:\s (\w ))? - an optional sequence of one or more whitespaces and then one or more word chars captured into Group 3
  • (,)? - an optional comma captured into Group 4.

The "$1(?2 $2)(?3 $3)"(?4,\n) replacement pattern replaces matches with:

  • " - a " char
  • $1 - Group 1 value
  • (?2 $2) - If Group 2 matches, adds space Group 2 value
  • (?3 $3) - If Group 3 matches, adds space Group 3 value
  • " - a " char
  • (?4,\n) - If Group 4 matches, adds comma and newline char.

See the demo screenshot:

enter image description here

CodePudding user response:

Well notepad is so rich in features, this code may be an alternative for some other editors too, that simply support Regular Expression ....
Find: \s*(header)\s*(.*?)[,\n]
Replace All: "$1 $2",\n

Attachment after Replace All

enter image description here

  • Related