I need help with this re.sub
function.
For example, if I wanted to replace “string” with “abc”
but I want #include <string.h>
to remain the same so that the syntax does not get disturbed and all the other “string” variables get replaced with “abc”. How can I write the re.sub
function?
The Python code is:
JavaScript
x
2
1
result = re.sub("string","abc", test_str, 0)
2
test_str
is this
JavaScript
1
16
16
1
#include <string.h>
2
#include <stdlib.h>
3
4
char *Function(unsigned char *string, unsigned char *key)
5
{
6
unsigned int i, j;
7
8
for (i = 0; i < s(string); i++)
9
{
10
11
string[i] = ~ string[i];
12
}
13
14
return string;
15
}
16
Advertisement
Answer
Use
JavaScript
1
2
1
result = re.sub(r"bstringb(?!.hb)", "abc", test_str)
2
See proof
Explanation
JavaScript
1
20
20
1
--------------------------------------------------------------------------------
2
b the boundary between a word char (w) and
3
something that is not a word char
4
--------------------------------------------------------------------------------
5
string 'string'
6
--------------------------------------------------------------------------------
7
b the boundary between a word char (w) and
8
something that is not a word char
9
--------------------------------------------------------------------------------
10
(?! look ahead to see if there is not:
11
--------------------------------------------------------------------------------
12
. '.'
13
--------------------------------------------------------------------------------
14
h 'h'
15
--------------------------------------------------------------------------------
16
b the boundary between a word char (w)
17
and something that is not a word char
18
--------------------------------------------------------------------------------
19
) end of look-ahead
20