Regular Expression - Add a character before/after each word

纵饮孤独 提交于 2021-02-07 09:11:00

问题


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


回答1:


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:




回答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

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



来源:https://stackoverflow.com/questions/40614144/regular-expression-add-a-character-before-after-each-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!