I'm pretty new to regular expressions. I have a requirement to replace spaces in a piece of multi-line text. The replacement rules are these:
- Replace all spaces at start-of-line with a non-breaking space (
) - Replace any instance of repeated spaces (more than one space together) with the same number of non-breaking-spaces
- Single spaces which are not at start-of-line remain untouched
I used the Regex Coach to build the matching pattern:
/( ){2,}|^( )/
Let's assume I have this input text:
asdasd asdasd asdas1 asda234 4545 54 34545 345 34534 34 345
using a php regex replace function (like preg_replace()
) I want to get this output:
asdasd asdasd asdas1 asda234 4545 54 34545 345 34534 34 345
I'm happy doing simple text substitutions using regular expressions, but i'm having trouble working out how to replace multiple-times inside the match in order to get the output i desire.