RegEx bug? | SoloLearn: Learn to code for FREE!

+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?

4/20/2017 3:17:25 PM

Supersebi3

21 Answers

New Answer

+10

actually, I just got an idea for it, but its too late now, gonna try it tomorrow

+10

My sample program was one that generated the ascii char 'E', and trust me, that was the best E I ever saw

+9

Lemme try

+9

I can leave my code here if you want

+9

I coded in an external app, ill put it here as private

+9

https://code.sololearn.com/cFNXvHsW1xUK/?ref=app

+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...

+9

the replacing works perfectly, but theres another seperate bug that cant be fixed...

+9

I DID IT! https://code.sololearn.com/cPdLTp5l2w7O/?ref=app

+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.

+8

Actually I will update, I managed to get it all into the function

+7

@Kuba how do you... rawify an unknown string?

+7

nope doesnt work, anyways I gave up on this, now trying it in ruby

+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

+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"\[#>#\+#\+#\+#\+#\+#\+#\+#<#\-#\]"

+4

No way, man! I'll get to it in the evening... 🐍 Check back tomorrow, if you will ;)

+4

Yep, pls publish it on your code list or let me know the link to it, if private.

+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?

+4

Amazing! I just tried it with the Hello World and it worked perfectly 👏

+4

code = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."