[OS X TeX] Find and replace with regular expressions

Eric Jablow ejablow at cox.net
Mon Nov 19 13:44:16 EST 2007

---- Art Werschulz <agw at comcast.net> wrote: 
> You'd need to make sure that the dollar signs aren't preceded by  
> backslashes.  So if you replace "$" by "[^\\]$" in the original  
> regexp, you should be okay.  Please note that I have not tested this  
> out, and so it may need tweaking.  Don't try this on your only copy  
> of the file!!!

I'm sorry that I hadn't read the post I replied to fully.  He did make the same point I did.  The regex [^\\]\$ has a problem however; it never matches at the beginning of the searched text!  Suppose you aren't using multi-line mode.  Then, a $ at the beginning of a line isn't preceded by anything.  Furthermore, the character before would have to be captured too so it could be replaced by itself.  I'm guessing this Perlish regex would work:

    (?<![^$\\])        # Search for an expression not preceded by a dollar sign or backslash,
    [$]                    # Followed by a single $.
    (                       # Capture the content
        (?:            # which we don't capture
       [^$] |         # consisting a non-dollar sign,
        \\\$)         # or an (escaped)  backslash and an (escaped) $
   +                   # repeated at least once
    )                  # Finish with the capture
   [$]                  # Followed by the closing dollar sign
} xm                      # Free-form regex and multiline mode.

I prefer [$] to \$ because I don't have to esacpe any of the characters with backslashes.

More information about the MacOSX-TeX mailing list