3

Using Notepad++ and replace function, I tried to add a symbol "+" or "[" before each word of my list.

Example of list :

  • blue car
  • red car big
  • red car small
  • green car big
  • green car small

I'm looking for the following result :

  • +blue +car
  • +red +car +small
  • +red +car +big
  • .. etc

I know how to add a character befor each line... but I cannot find the way to add it in front of every word without using replace "blue" to "+blue".

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
B4b4j1
  • 430
  • 2
  • 7
  • 24

2 Answers2

10

A cross platform solution should be

Search: \b\w+\b (or \b[[:alpha:]]+\b)
Replace: +$&

Search pattern details:

  • \b - a leading word boundary
  • \w+ - 1 or more word chars (if [[:alpha:]]+ is used, 1+ letters)
  • \b - a trailing word boundary

Replacement details: + is a literal plus, and $& is the backreference to the whole match.

See the screenshot:

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Thank you for the clear explanation... Still have some work to manage the regex ! – B4b4j1 Dec 14 '16 at 11:22
  • 2
    You mean "learn" :) ? Note that most of the online regex testers should be trusted to a certain degree since they do not usually support the NPP way of handling regex. Check [Boost regex syntax](http://www.boost.org/doc/libs/1_62_0/libs/regex/doc/html/boost_regex/syntax.html), and always test in Notepad++. – Wiktor Stribiżew Dec 14 '16 at 11:25
  • Yes learning is the key ! I find myself using regex more and more to work with big words list and tables. But I lack of time to deepen all subjects. Thx again for help ! – B4b4j1 Dec 14 '16 at 11:30
2

(see screenshot below)

  • open the Find/Replace dialog (Ctrt+H)
  • in the Find input, enter this regex: (\b\w) which means "word boundary followed by a letter"
  • in the Replace with input, enter this replacement: +\1 which means "put a + sign followed by whatever was matched between the regex parenthesis"
  • click Show advanced options checkbox
  • click Search with regular expressions radio button
  • then hit Replace button as many times as you want, or use Replace all for once

enter image description here

EDIT: for Windows is pretty much the same (see the find/replace dialog http://sqlblog.com/blogs/jamie_thomson/image_1AFC2B61.png) the Regular Expression option is at the bottom left

arhak
  • 2,488
  • 1
  • 24
  • 38
  • yes I tried it, and works for me. See screenshot. Did you made all the regex options the same as displayed dialog? – arhak Nov 15 '16 at 16:05
  • OK, the `\b` doesn't seem to work right in Windows NPP. – AbraCadaver Nov 15 '16 at 16:07
  • added Windows answer as well, but screenshot is taken from the web, so options are not marked properly – arhak Nov 15 '16 at 16:09
  • I tested on Windows. Using `red car small` replace `(\b\w)` with `+\1` yields: `+r+e+d +c+a+r +s+m+a+l+l` – AbraCadaver Nov 15 '16 at 16:11
  • not sure why, but in that case, change it to `(\b\w+)` and should sort it out – arhak Nov 15 '16 at 16:12
  • 1
    No, in Windows NPP, `\b\w` does not work, see [Notepad++ Capitalize Every First Letter of Every Word](http://stackoverflow.com/questions/31952353/notepad-capitalize-every-first-letter-of-every-word/31953204#31953204). – Wiktor Stribiżew Nov 15 '16 at 16:13
  • 1
    That's what i needed... thank you ! And sorry for late answer. – B4b4j1 Dec 14 '16 at 11:20