• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Removing all “more than two spaces” but leaving “one space only”

Resolved: Removing all “more than two spaces” but leaving “one space only”

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Small question regarding some string manipulation please.
I have a file which lines sometimes contains a large number of spaces (the character space), always more than two.
But it also contains valid phrases, where words are separated by one space only.
Example: ['a good sentence where words are separated by one character space only']
Above is the example for one line, the files contains many of those lines. Each line therefore also has a “line break” character which I would like to preserve.
What I would like to achieve is pretty simple, in a “find & replace” option which allow me to input a regex, I would like to give it a regex which says:
For all the sequences of spaces that are more than 2, just remove them. For the normal spaces, 1 space only, keep them. Keep also the line breaks.
Expected result:
['a good sentence where words are separated by one character space only']
I tried this regex: \ \.*
But it is not yielding any result.
This question is for a simple text editor which offers the option replace in text by regular expression. I am not using any programming language to do so.
May I ask what would be the correct regex in this context please?
Thank you

Answer:

You need to:
\s{2,} replace to (1 space)
or
{2,} (space two or more times) replace to (1 space).
\s is (space), \n (new line), \t (tab) and other space chars

If you have better answer, please add a comment about this, thank you!

regex regexp-replace
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Vaadin 14.9 – Redirect on Session Destroy Event

26/03/2023

Resolved: How to separate multiple answers in one column, for multiple columns, by creating extra columns

26/03/2023

Resolved: How to set consistent decimal separators in R data frame?

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.