Как использовать preg_replace для удаления повторяющихся символов с пробелами вокруг ⇐ Php
Как использовать preg_replace для удаления повторяющихся символов с пробелами вокруг
I have multiple strings and I need to remove repeated chars. For example: the string here abbbbbc x should become here abc x or the string test jjka should become test jka.
After studying, I came up with the code below which works fine (it uses PHP but you can use any language):
echo preg_replace("/([a-z])\\1+/","$1","test ajjjo new"); The code above will output test ajo new which is great!
My problem now, is that I need to only replace the repeated chars if they are inside a word or at the beggining of end of the word. For example: I need the string here bbb cca to become here bbb ca and the string test hjjjja ppp to become test hja ppp. I tried negating the (space) and ^ and $ but it all becomes a mess pretty fast.
How would you recommend me?
Источник: https://stackoverflow.com/questions/780 ... ces-around
I have multiple strings and I need to remove repeated chars. For example: the string here abbbbbc x should become here abc x or the string test jjka should become test jka.
After studying, I came up with the code below which works fine (it uses PHP but you can use any language):
echo preg_replace("/([a-z])\\1+/","$1","test ajjjo new"); The code above will output test ajo new which is great!
My problem now, is that I need to only replace the repeated chars if they are inside a word or at the beggining of end of the word. For example: I need the string here bbb cca to become here bbb ca and the string test hjjjja ppp to become test hja ppp. I tried negating the (space) and ^ and $ but it all becomes a mess pretty fast.
How would you recommend me?
Источник: https://stackoverflow.com/questions/780 ... ces-around
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как использовать preg_replace для удаления повторяющихся символов с пробелами вокруг
Anonymous » » в форуме Php - 0 Ответы
- 8 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Почему `text.replace('\n', ' ')` не заменяет символы новой строки пробелами?
Anonymous » » в форуме Python - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-