0

i have this example string this is part of the section in the string forward slash must be at the end

declare @mytable nvarchar (50) = 'one_table';
declare @mydata nvarchar(255)  = 'C:\foo\bar\test\date\';
--DECLARE @test nvarchar (50) = '20140101';

no i like to replace the line forward slash must be at the end:

declare @mydata nvarchar(255)  = 'C:\foo\bar\test\date\';

with this line :

declare @mydata nvarchar(255)   = 'D:\test\test\foo\b\';

i try to replace it like this db_script is list that conatins conatins the script :

 pattern = '.*declare.*\@mydata.*nvarchar\(255\).*$'
 dest = 'declare @mydatanvarchar(255)  = \'D:\test\test\foo\b\';'

 for command in db_script:
        command = re.sub(pattern, dest, command)
        print(command)

even if i isolate the line like this forward slash must be at the end:

ll = 'declare @mydata nvarchar(255) = \'C:\foo\bar\test\date\';'
    pattern = '.*declare.*\@mydata.*nvarchar\(255\).*$'
    dest = 'declare @mydata nvarchar(255)  = \'D:\test\test\foo\b\';'
    command = re.sub(pattern, dest, ll)

i can't catch the result in command.
if i test it in regexp tester all working what I'm doing wrong?

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 1
    You need to have double literal backslash in the replacement to replace with a single literal backslash. That is, if you use a regular string literal, 4 backslashes, ``\\\\``. Or, 2 backslashes in a raw string literal, see https://ideone.com/Sm2xAy – Wiktor Stribiżew Jul 12 '20 at 16:16
  • very strange, if i use my own "real" strings it do not work, how do i set the replaced line back in to the big string? – user63898 Jul 12 '20 at 16:29
  • What do you mean by "own "real" strings"? You only provided some regular string literals in the question, you never showed the real texts. Have you tried `print(ll)`? `print(dest)`? – Wiktor Stribiżew Jul 12 '20 at 16:31
  • getting : SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 42-43: – user63898 Jul 12 '20 at 16:32

0 Answers0