<pre><code>$output = preg_replace("|(/D)(/s+)(/d+)(;)|", "//1,//3;", $output);
</code></pre>

<p>I'm trying to replace all alphabetical character followed by one or more whitespace characters (tabs and/or spaces) followed by one or more numerical characters followed by a semicolon with the alphabetical character followed by a comma followed by the numerical digits and then the semicolon.</p>

<p>I'll provide an example:</p>

<p>Start:</p>

<pre><code>hello world      1007;
</code></pre>

<p>End:</p>

<pre><code>hello world,1007;
</code></pre>

[{'answer_id': 53988, 'author': 'T Percival', 'author_id': 954, 'author_profile': 'https://Stackoverflow.com/users/954', 'pm_score': 2, 'selected': False, 'text': "<p>Should those forward-slashes be backslashes? You'll need to escape them for PHP too unless you change your double-quotes to single-quotes.</p>\n"}, {'answer_id': 53993, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 4, 'selected': True, 'text': '<p>The two <code>|</code> at the start and end probably are incorrect - and should both be forward-slashes.</p>\n\n<p>All other forward slashes should be backward slashes (and need escaping).</p>\n\n<p>And since PHP 4.04 <code>$n</code> is the preferred way of referring to a capture group.</p>\n\n<pre><code>$output = preg_replace("/(\\\\D)\\\\s+(\\\\d+;)/", "$1,$2", $output);\n</code></pre>\n\n<p>If you use single quotes you don\'t need to escape your backslashes:</p>\n\n<pre><code>$output = preg_replace(\'/(\\D)\\s+(\\d+;)/\', \'$1,$2\', $output);\n</code></pre>\n'}, {'answer_id': 54018, 'author': 'Alan Storm', 'author_id': 4668, 'author_profile': 'https://Stackoverflow.com/users/4668', 'pm_score': 1, 'selected': False, 'text': '<p>You want backslashes in the regular expression, not forward slashes.  The starting and ending pipes <strong>are</strong> needed (or another delimiter for the regex)</p>\n\n<pre><code>$x = "hello world      1007;";    \necho preg_replace(\'|(\\D)(\\s+)(\\d+)(;)|\',\'$1,$3\',$x);\necho preg_replace(\'/(\\D)(\\s+)(\\d+)(;)/\',\'$1,$3\',$x);\necho preg_replace(\'{(\\D)(\\s+)(\\d+)(;)}\',\'$1,$3\',$x);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/53965', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/572/']