1

In Notepad++ RegEx, I want to search for all strings starting with a tilde and terminating with \n, and within each match replace all spaces with non-breaking spaces.

That is, I want to find all instances of \~.*^, and within the resulting $0, replace all [Space]s with [Non-breaking Space].

Is this possible?

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
Kapitano
  • 163
  • 13
  • what do you mean by non breaking space? – karthik manchala Jun 06 '15 at 13:47
  • @karthikmanchala blanks and tabstops I assume – dognose Jun 06 '15 at 13:47
  • yes it's possible but it would help if you gave an example line that you want to run the regex on (and expected output) – jcuenod Jun 06 '15 at 13:47
  • @karthikmanchala  ? – jcuenod Jun 06 '15 at 13:48
  • Not sure if notepad++ regex engine is powerful enough for this sort of thing but here is a php solution http://stackoverflow.com/questions/18581227/regex-to-replace-all-occurrences-of-single-character-within-specific-tokens – CrayonViolent Jun 06 '15 at 13:56
  • A non-breaking space character looks like an ordinary space, but is treated by text editors as a non-delimiter. It's Unicode U+00A0, HTML   and Alt+255 – Kapitano Jun 06 '15 at 13:59
  • Example: 'God is dead' ~ Frederich Nietzsche. The space before the ~ would remain a normal space. The spaces after it and before the N would become non-breaking spaces. Thus "~ Frederich Nietzsche" would become a single "word", from the users point of view. – Kapitano Jun 06 '15 at 14:05

2 Answers2

2

You can use the following to match:

(?:~|\G(?<!^))\S*\K\s

Or try:

(?:~|\G(?!^))\S*\K[ ]

And replace with non breaking space

See DEMO

Credits

Community
  • 1
  • 1
karthik manchala
  • 13,492
  • 1
  • 31
  • 55
  • Sorry, in N++ that just replaces all spaces. Maybe a weakness in N++'s implementation of regex. I'll try to reverse engineer from the demo. – Kapitano Jun 06 '15 at 14:15
  • Good idea! Maybe not working for @Kapitano because of `\s` at the end. Try `(?:~|\G(?!^))\S*\K[ ]` – Jonny 5 Jun 06 '15 at 14:56
  • @Jonny5 hmm.. maybe.. I tried it with `\s` in N++ and it works.. in anycase.. updated with your suggestion too.. :) – karthik manchala Jun 06 '15 at 15:01
0

With fixed-width pattern lookbehind regex engines (e.g., Perl):

s/(~.*?) {2,}/\1 /g

with variable-width pattern lookbehind regex engines:

s/(?<=\~.*) {2,}/  /g

or with Vim:

s/\(\~.*\)@<= \{2,}/  /g

I'm not sure about Notepad++. Hopefully you can work it out based on the above.

bjfletcher
  • 11,168
  • 4
  • 52
  • 67