Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to match a character only if not followed by another character, then replace just that initial character

Tags:

regex

For instance,

(/=[^>]/, '═')

I'd like to keep that match, but only replace the equals sign with the double-horizontal-line. As it is, it matches any '=' that is followed by anything that isn't a '>' but then replaces both the '=' and the following character with the replacing character, I want to keep the following character, but replace just the '='. This is in ruby, if it makes any syntactic difference.

Example input:

= render :partial => 'file'

First = should be converted, second should be preserved

like image 848
aperture Avatar asked Dec 13 '22 18:12

aperture


1 Answers

Depending on your regex library (I don't know Ruby), you may be able to use zero-width assertions:

/=(?!>)/

Note that this regex is slightly different to your regex, but it matches the description you gave in the title better. It will match any = that isn't followed by >. This includes matching an = at the end of the text, which your version won't match.

like image 157
Marcelo Cantos Avatar answered May 19 '23 00:05

Marcelo Cantos