Sublime Forum

Regex: Remove every two or more spaces between specific tags and leave just a space instead

#1

hello, I want to remove every two or more spaces between specific tags and leave just a space instead:

For example:

<p class="text_obisnuit">  The context of articles,   stories, and conversations helps you        figure out and understand the meaning   of English words in the text that are new to you.   </p>

My desire output:

<p class="text_obisnuit">The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.</p>

I tried something but it did not work

(?<=<p class="text_obisnuit">)\s*|\s*(?=</p>)

0 Likes

#2

use find and replace

find regex: ' {2,}'
replace with: ' '

0 Likes

#3

??? don’t understand. Besides, I want to select a specific tag <p class="text_obisnuit">...</p> and to remove all space and tabs from it

0 Likes

#4

Using regex for this isn’t exactly ideal since 1) it is next to impossible to parse html with regex and 2) there is no looping within a match & thus can only replace one thing at a time.

However, if you are confident that <p class="text_obisnuit">.*?</p> can always and correctly identify the range you want changes in, this will work:

  1. open the find & replace pane
  2. paste this in find: (<p class="text_obisnuit">.*?) {2,}(.*?</p>)
  3. paste this for replace: $1 $2
  4. click Replace or Replace All until no more results are found, keep in mind it will only fix one issue per section
0 Likes

#5

FIND THE SOLUTION !!!

SEARCH:

(?s)(?:\G|<p class="text_obisnuit">)(?:(?!<|>).)*?\K(?:(^\h+)|\h+$|(?<=>)\h+|\h+(?=</p>)|(\h{2,})(?=[^<\h]))

REPLACE BY:

(?1$0)(?2\x20)

0 Likes