How can you format a line of this type К У Л Ь Т У Р А Р Е Ч И И Д Е Л О В О Е О Б Щ Е Н И Е Here is the КУЛЬТУРА РЕЧИ И ДЕЛОВОЕ ОБЩЕНИЕ ? Spaces are random, but between letters they are always the same, and the space between words is always greater than between letters.
3 answers
It is necessary to make a replacement by the following expression for emptiness (ie, delete):
(?<=\S)\s(?=\S)|\s(?=\s) regex101.com example
here we highlight any spaces before and after which there are no spaces or spaces, after which there is a space. Unfortunately, the expression is designed for exactly one whitespace character between letters, if the number of spaces is different, then without first calculating the lengths of the spaces and substituting these lengths into the expression is indispensable. In this case, the length can be obtained in advance using such an expression ( +)(?=.*?(\1 +)) . The first capture group will return a shorter space, the second capture group a longer one. If the expression does not find anything, then all the spaces in the string are the same length.
PS If it is necessary to replace strictly spaces, but not any whitespace characters, then it is necessary to replace in the expression \s with the actual spaces, and \S with [^ ] .
\s=[ \f\n\r\t\v]- Visman- @Visman I know, but decided to play it safe, suddenly there are not only the spaces themselves. - Mike
- Thanks, the shortest solution, through match I found and just replaced it with what I need - 100ROZH
Calculate the size of the gap between words and letters. Unique sizes will be - 2 (it’s just not clear in advance which ones they are). Then remove the minimum in size and replace the maximum in 1 space. Regulars here will not help much. Just find everything by \s+ - it will be 2 huge heaps, but there will be only 2 sizes in it.
- thanks, there were some such thoughts, I just thought that it was possible to perform these operations somehow regularly - 100ROZH
First you need to replace all single spaces followed by the letter - \s{1}(.\S{1,}) replaced by $1 And then replace several spaces by 1 - \s{1,} replaced by
On php, this is first preg_replace("/\s{1}(.\S{1,})/i", "$1", $input); and then preg_replace("/\s{1,}/i", " ", $input);
\s=[ \f\n\r\t\v]- Visman- Duck there may be more than one space, a condition such that there are as many spaces as you like, but an equal amount - 100ROZH