Python Forum
Problem with the '|' character?
Thread Rating:
  • 3 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with the '|' character?
#1
Hello all, I'm trying to use the following method to find the "|" character in the following string as a little test:
[m.start() for m in re.finditer('|', 'fjd|fhjdkal|fgdha|ads')]
Strangely it returns all of the character indexes, from 0 to 21. Why? It seems to work normally with other characters.
Even more, strangely, it still returns a similar output when there is not even a "|" in the string:
[m.start() for m in re.finditer("|", 'abc')]
Output:
[0, 1, 2, 3]
Why? And how can I fix this problem? Thanks! I really need this for my new esolang!
Reply
#2
| is a special character in RegEx. Escape it.
here is online tester where you can experiment with regex https://regex101.com/
Reply
#3
Thanks! It works perfectly now!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 3,388 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,158 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,731 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 6,172 Mar-25-2019, 12:54 PM
Last Post: silfer
  Problem with character sets Pedroski55 4 3,709 Mar-04-2019, 02:35 AM
Last Post: snippsat
  SyntaxError: unexpected character after line continuation character Saka 2 18,547 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020