Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Add a character before/after each word

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".

like image 834
B4b4j1 Avatar asked Oct 16 '25 19:10

B4b4j1


1 Answers

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

like image 74
Wiktor Stribiżew Avatar answered Oct 18 '25 11:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!