Perform regex (replace) in an SQL query

后端 未结 6 1546
闹比i
闹比i 2020-12-01 11:08

What is the best way to replace all \'<\' with < in a given database column? Basically perform s/<[^;]/</gi

Notes

6条回答
  •  情歌与酒
    2020-12-01 11:48

    If MSSQL's regex flavor supports negative lookahead, that would be The Right Way to approach this.

    s/<(?!;)/</gi
    

    will catch all instances of < which are not followed by a ; (even if they're followed by nothing, which [^;] would miss) and does not capture the following non-; character as part of the match, eliminating the issue mentioned in the comments on the original question of that character being lost in the replacement.

    Unfortunately, I don't use MSSQL, so I have no idea whether it supports negative lookahead or not...

提交回复
热议问题