Search code examples
regexnotepad++

How to convert `Multiple words` into `multiple-words as "Multiple words"`?


Say I have a text:

Multiple words
In yellow and in blue

How can I make it to this?

multiple-words as "Multiple words" 
in-yellow-and-in-blue as "In yellow and in blue" 

So far my best try is this:

  • Find: (.*)
  • Replace: -\1- as "\1"

To have it in the form of -Multiple words- as "Multiple words". The - is to mark the boundary easier. But I can't tell it to only replace the spaces in between the boundaries.


Solution

  • You can do that in several steps:

    1. Search for ^.*$ and replace with $& as "$&": it just wraps all lines with quotes.
    2. Search for (?:\G|^\S*)\K\h+(?=.* as ".*"$) and replace with -: it replaces all horizontal whitespaces (\h) from the start of the line till a as "..." pattern.
    3. Search for (?:\G-|^)[^-]+(?=\S* as ".*"$) and replace with \L$&: it will turn to lowercase all chunks of non-hyphen chars from the start of string till as "..." pattern.

    Note \L, \U and other case changing operators do not work with non-ASCII letters in Notepad++.