1

I messed up my html somehow and all of my html now looks like this with a whitespace between each character

< d i v  c l a s s = " c o l - x s - 1 2   c o l - s m - 6 " >                               
< d i v  c l a s s = " f o r m - g r o u p   c o l - x s - 1 2 "> 
< d i v  c l a s s = " r a d i o "> 
< l a b e l  c l a s s = " r a d i o - l a b e l  "> 

It is over 2000 lines of code. I need to go back looking like this

<div class = "col-xs-12 col-sm-6">                               
<div class = "form-group col-xs-12" > 
<div class = "radio" > 
<label class = "radio-label" > 

What regular expression can I use with notepad++ to fix this?

fjellfly
  • 388
  • 1
  • 13
CodeMan03
  • 570
  • 3
  • 18
  • 43
  • How on earth did you manage that? I'm not familiar with Notepad++ regex syntax, but you want one that will find any two non-space characters separated by a space and just delete the space (leaving the two characters). You can then go through and change double spaces to single. – lurker Nov 13 '15 at 01:15
  • I tried to do a simple find and replace all in Visual Studio and it came back 40000 occurrences changed. I couldn't even do an undo. – CodeMan03 Nov 13 '15 at 01:26
  • I've been there, and I feel your pain. – Steve Clanton Nov 13 '15 at 01:38
  • In NP++ you could also try `(?<=[^ ])[ ]` and replace with empty. – bobble bubble Nov 13 '15 at 05:26

2 Answers2

2

Notepad++ doesn't support very advanced regex. Your best bet is to do a find and replace to replace two consecutive spaces with a special character that doesn't appear in your page, for example ~.

< d i v~c l a s s = " c o l - x s - 1 2~ c o l - s m - 6 " >~ 
< d i v~c l a s s = " f o r m - g r o u p~ c o l - x s - 1 2 "> 
< d i v~c l a s s = " r a d i o "> 
< l a b e l~c l a s s = " r a d i o - l a b e l~"> 

Now, you can replace all the spaces with the empty string to remove them:

<div~class="col-xs-12~col-sm-6">~
<div~class="form-group~col-xs-12">
<div~class="radio">
<label~class="radio-label~">

We have to replace the special character (~) with a space now, and we're done:

<div class="col-xs-12 col-sm-6"> 
<div class="form-group col-xs-12">
<div class="radio">
<label class="radio-label ">

Since you mention you have Visual Studio, this is actually easier to solve with Visual Studio if you have it, since you can use a more advanced regex: Find (\S)\s or (.)\s and replace with $1.

If you have the TextFX plugin installed, you can run fairly complex regular expressions in notepad++ through TextFX -> TextFX Quick -> Find/Replace. You can run (\S)\s or (.)\s as the expression to find and use \1 for the replace.

Steve Clanton
  • 4,064
  • 3
  • 32
  • 38
  • That did it. You are a life saver! – CodeMan03 Nov 13 '15 at 01:46
  • Mark as answer and upvote are the best way to say thanks. Glad you could recover. – Steve Clanton Nov 13 '15 at 01:48
  • 2
    np++ [uses PCRE](http://docs.notepad-plus-plus.org/index.php/Regular_Expressions) (which is advanced) but some features are not available. – bobble bubble Nov 13 '15 at 05:22
  • 1
    Notepad++ has powerful regular expressions, perhaps you are using an old version. Notepad++ will handle the expressions at the end of your answer. See http://stackoverflow.com/questions/11389466/multiple-word-search-and-replace-in-notepad/16104946#16104946 for documentation links. – AdrianHHH Nov 13 '15 at 12:03
  • I am using 6.8.3. PCRE (most of it)is available using a plugin: `TextFX -> TextFX Quick -> Find/Replace`. However, in the find and replace dialog box, the syntax is pretty limited. The documentation linked on the question is for a C++ library. Here is the link to np++ documentation that says "if you have the plugin installed." http://docs.notepad-plus-plus.org/index.php/Regular_Expressions Point taken though, I should expand the answer to show how to do this replacement with the TextFX replace feature to be complete. – Steve Clanton Nov 13 '15 at 12:14
1

How about:

Find what: (?<=\S)\s(?=\S)
Replace with: NOTHING

This will replace every space \s that is between two non spaces \S.

(?<=\S) is a positive lookbehind
(?=\S) is a positive lookahead

Toto
  • 89,455
  • 62
  • 89
  • 125