2

I want to split my string into pieces but every some text and a special character. I have a string: str = "ImEmRe@b'aEmRe@b'testEmRe@b'string"
I want my string to be split every EmRe@b' characters as you can see it contais the ' and that's the problem.
I tried doing re.split(r"EmRe@b'\B", str), re.split(r"EmRe@b?='\B", str) and also I tried both of them but without the r before the pattern. How do I do it? I'm really new to regular expressions. I would even say I've never used them.

Qiasm
  • 356
  • 6
  • 15

1 Answers1

1

Firstly, change the name of your variable, since str() is a built-in Python function. If you named your variable word, you could get a list of elements split by your specified string by doing this:

>>> word = "ImEmRe@b'aEmRe@b'testEmRe@b'string"
>>> word
"ImEmRe@b'aEmRe@b'testEmRe@b'string"
>>> word.split("EmRe@b'")
['Im', 'a', 'test', 'string']

Allowing you to use them in many more ways than just a string! It can be saved to a variable, of course:

>>> foo = word.split("EmRe@b'")
>>> foo
['Im', 'a', 'test', 'string']
peki
  • 669
  • 7
  • 24