+ 8

RegEx bug?

While I was coding a brainfuck interpreter, I wanted to use RegEx. I wanted to use the re.sub() function to remove a substring from the full code. What I did: x = re.sub(substr, "", x, 1) Values at this point (with a sample code) are: substr = "[#>#+#+#+#+#+#+#+#<#-#]" x = "+#+#+#+#+#+#+#+#+#[#>#+#+#+#+#+#+#+#<#-#]#>#-#.#+#" I use the # character for debugging What happens: instead of replacing the occurence of substr in x with "", it does it to the very first symbol of x. Why? What else could I do?

20th Apr 2017, 3:17 PM
Supersebi3
Supersebi3 - avatar
21 RĂ©ponses
+ 10
actually, I just got an idea for it, but its too late now, gonna try it tomorrow
21st Apr 2017, 8:44 PM
Supersebi3
Supersebi3 - avatar
+ 10
My sample program was one that generated the ascii char 'E', and trust me, that was the best E I ever saw
23rd Apr 2017, 9:52 PM
Supersebi3
Supersebi3 - avatar
+ 9
Lemme try
21st Apr 2017, 3:54 PM
Supersebi3
Supersebi3 - avatar
+ 9
I can leave my code here if you want
21st Apr 2017, 4:03 PM
Supersebi3
Supersebi3 - avatar
+ 9
I coded in an external app, ill put it here as private
21st Apr 2017, 4:19 PM
Supersebi3
Supersebi3 - avatar
21st Apr 2017, 4:24 PM
Supersebi3
Supersebi3 - avatar
+ 9
I already inserted the backslashes to solve the RegEx problem, but it still doesnt work. I know why, and im pretty sure it cant be fixed...
21st Apr 2017, 8:40 PM
Supersebi3
Supersebi3 - avatar
+ 9
the replacing works perfectly, but theres another seperate bug that cant be fixed...
21st Apr 2017, 8:44 PM
Supersebi3
Supersebi3 - avatar
23rd Apr 2017, 9:47 PM
Supersebi3
Supersebi3 - avatar
+ 8
I'd have to see the code to help you. But off the top of my head - maybe try preceeding the strings with r to mark them as raw, maybe it helps.
21st Apr 2017, 6:40 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 8
Actually I will update, I managed to get it all into the function
23rd Apr 2017, 10:29 PM
Supersebi3
Supersebi3 - avatar
+ 7
@Kuba how do you... rawify an unknown string?
21st Apr 2017, 1:07 PM
Supersebi3
Supersebi3 - avatar
+ 7
nope doesnt work, anyways I gave up on this, now trying it in ruby
21st Apr 2017, 3:57 PM
Supersebi3
Supersebi3 - avatar
+ 7
I tested it with some other programs and made some improvements, not gonna add them here though because they involve the executing, not just the converting
23rd Apr 2017, 10:16 PM
Supersebi3
Supersebi3 - avatar
+ 5
Got it, man. In your code the substr is a string which contains regex-wise special characters. Unfortunately it seems like *each* of them has to be escaped. If you use the following, it will be found and replaced with what you want: substr = r"\[#>#\+#\+#\+#\+#\+#\+#\+#<#\-#\]"
21st Apr 2017, 8:14 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
No way, man! I'll get to it in the evening... 🐍 Check back tomorrow, if you will ;)
21st Apr 2017, 4:03 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
Yep, pls publish it on your code list or let me know the link to it, if private.
21st Apr 2017, 4:18 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
Weird, works for me :) I tried with different char strings and it only worked after all special elements were escaped. You got another solution?
21st Apr 2017, 8:43 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
Amazing! I just tried it with the Hello World and it worked perfectly 👏
23rd Apr 2017, 9:51 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
code = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
23rd Apr 2017, 9:53 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar