Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6866061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:07:58+00:00 2026-05-27T03:07:58+00:00

I am trying to match a pipe character in a string using a Python

  • 0

I am trying to match a pipe character in a string using a Python regex and I can’t seem to get it to match. I’ve boiled it down to a simplified version.

Let’s say I am looking for the sequence z|a in a string. Here are some possible regexes and the results:

>>> import re
>>> re.match(r'|', 'xyz|abc')
<_sre.SRE_Match object at 0x2d9a850>
>>> re.match(r'z|', 'xyz|abc')
<_sre.SRE_Match object at 0x2d9a780>
>>> re.match(r'|a', 'xyz|abc')
<_sre.SRE_Match object at 0x2d9a850>
>>> re.match(r'z|a', 'xyz|abc')
>>> re.match(r'z\|a', 'xyz|abc')
>>> re.match(r'z\\|a', 'xyz|abc')
>>> re.match(r'z\\\|a', 'xyz|abc')
>>> re.match(r'z[|]a', 'xyz|abc')
>>> 

So I can match with |, |a and z| but I can’t find a way to match z|a. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:07:59+00:00Added an answer on May 27, 2026 at 3:07 am

    re.match() is looking for a match at the start of the string. Use re.search() instead.

    The patterns you have that match are matching the empty string. i.e. r’|’ is empty string or empty string, r’z|’ is z or empty string and ‘|a’ is empty string or a. all of those will match on any string.

    >>> re.match('z\\|a', 'xyz|abc')
    >>> re.search('z\\|a', 'xyz|abc')
    <_sre.SRE_Match object at 0x02BF2BB8>
    >>> re.search(r'z\|a', 'xyz|abc')
    <_sre.SRE_Match object at 0x02BF2BF0>
    

    More generally you can use re.escape() on a literal string that you need to include in the middle of a more complex regular expression to avoid having to figure out how many backslashes you need to unescape things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to match ONLY the first character in the sample below. Sample string: C/C++/Objective
Trying to match string like this: /2011/10/Lorem-ipsum-dolor-it-amet-consectetur-adipisicing /2011/10/Lorem-ipsum-dolor-it-amet-consectetur-adipisicing/ and /2011/10/4545 /2011/10/4545/ And get year,
I am trying to match some emoticon symbols using javascript regex. The regex doesn't
I'm trying to match elements with a name that is 'container1$container2$chkChecked' , using a
I'm trying to match the parts of a version number (Major.Minor.Build.Revision) with C# regular
I'm trying to match the following items in the string pcode : u followed
I am trying to match the text contents(character data) of an XML file with
I'm trying to match chunks of JS code and extract string literals that contain
I'm trying to match punctuations using regular expressions. Do I have to specify every
I'm trying to match an integer expression against character literals, and the compiler complains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.