regex replace spaces between dollar signs -


how can replace spaces between 2 dollar sign?

with regex work fine, can remove spaces between r , r.

\s(?![^\r]*(\r|$)) the result

but when use dollar sign instead r, doesn't work. maybe there special way dollar sign.

\s(?![^\$]*(\$|$)) result dollar sign

edit: programming language: php

the \s+(?!(?:(?:[^$]*\$){2})*[^$]*$) pattern suggested in 1 of comments involves lot of backtracking, highly inefficient , can cause program freeze.

here how in php (replacing space in between $ symbols hyphen):

$re = '~\$[^$]+\$~';  $str = "\$ words words \$ \$ words words \$ \$ words words \$ \$ words words \$";  $result = preg_replace_callback($re, function($m) {     return str_replace(" ", "-", $m[0]); }, $str); echo $result; 

see ideone demo

with \$[^$]+\$ pattern, match whole substrings between 2 dollar symbols, , inside preg_replace_callback, can further manipulate replacement applying str_replace matches.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -