regexgoogle-sheets

google sheet REGEXREPLACE


in google sheet ,i used regex method to extract and replace the A2[fmov s0, #1.00000000 ] to D2[fmov s0, ##0.975#0.975]. but the "[^#]*$" only work in extract value, in replace function, it produced wrong result as D2, the correct format should be D3[fmov s0, #0.975 ].

How should I edit my regex to get correct format as D3.

Thx in advance.

HEX REGEXEXTRACT(A2,"[^#]*$") B2*0.95 REGEXREPLACE(A2,"[^#]*$","#"&C2)
fmov s0, #1.00000000 1.0 0.95 fmov s0, ##0.975#0.975
fmov s0, #0.95

Solution

  • You pattern actually matches two time in your string, that is why you get replacement 2 times :) Please take a look at regex demo for this: Regex example

    This is because your pattern is not what you want really, [^#]*$ will match zero or more characters other than # until the end of the string, so in your case it matches 1.00000 of course, but it also then matches just end of string (second separate match, 0 length).

    What you need is pattern to match everything between # and end of string:

    #[^#]*$
    

    and you need to add one more # to replacement, as it will match starting # to ensure you won't random 0 length matches :)

    Or you could do positive lookbehind (if that's supported in Google sheets):

    (?<=#)[^#]*$
    

    Regex Demo