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

  • Home
  • SEARCH
  • 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 8719413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:47:32+00:00 2026-06-13T06:47:32+00:00

Code Iteration #2 Changing var1 to a raw string by using the stringVar =

  • 0

Code Iteration #2

Changing var1 to a raw string by using the stringVar = r'string' worked great. With the code below I am now getting an exception of:

Traceback (most recent call last):
  File "regex_test.py", line 8, in <module>
    pattern = re.compile(var2 + "(.*)")
  File "/usr/lib/python2.7/re.py", line 190, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.7/re.py", line 242, in _compile
    raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis

—

#!/usr/bin/python

import re

var1 = r'\\some\String\to\Match'
var2 = '\\\\some\\String\\'

pattern = re.compile(var2 + "(.*)")
found = pattern.match(var1, re.IGNORECASE)

if found:
    print "YES"
else:
    print "NO"

I am trying to include a variable in my regular expression. This question is related to this other question, but differs slightly by using a compiled pattern vs the variable within the match. According to everything I’ve read, the example code below should work.

#!/usr/bin/python

import re

var1 = re.escape('\\some\String\to\Match') # A windows network share
var2 = "\\\\some\\String\\"

print var1 # Prints \\some\\String\ o\\Match
print var2 # Prints \\some\String\

pattern = re.compile(var2)
found = pattern.match(var1 + "(.*)", re.IGNORECASE)

if found:
    print "YES"
else:
    print "NO"

When I print out my variables I am seeing some weird behavior. I thought the re.escape would escape all needed chars within a string.

When I execute the code in Python 2.7 on Ubuntu 12.4.1 I get the following exception

Traceback (most recent call last):
  File "regex_test.py", line 11, in <module>
    pattern = re.compile(var2)
  File "/usr/lib/python2.7/re.py", line 190, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.7/re.py", line 242, in _compile
    raise error, v # invalid expression
sre_constants.error: bogus escape (end of line)

What am I missing that is causing the exception to be thrown?

  • 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-06-13T06:47:33+00:00Added an answer on June 13, 2026 at 6:47 am

    re.escape is used for escaping a string to use as a regular expression, but you escape var1 and then use var2 as the regular expression.

    I think the following is what you are trying to accomplish:

    var1 = r'\\some\String\to\Match'
    var2 = re.escape('\\\\some\\String\\')
    pattern = re.compile(var2 + '(.*)', re.IGNORECASE)
    found = pattern.match(var1)
    

    Note that the r'\\some\String\to\Match' is a raw string literal, but you cannot use it for var2 since it needs to end in a backslash.

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

Sidebar

Related Questions

Anyone have any idea why the next cursor isn't changing in the below code?
Anyone have any idea why the next cursor isn't changing in the below code?
I'm writing some code so that at each iteration of a for loop it
I have some code that runs a model in a loop. Each iteration of
Let's say I keep only working tested code in trunk . Each iteration is
I'm getting a strange NullPointerException, evidently thrown by the following line of code: Iterator<Note>
I just came across the following code: for(Iterator<String> iterator = stuff.iterator(); iterator.hasNext();) { ...
I have the following C++ code: typedef istream_iterator<string> isi; // (1) vector<string> lineas(isi(cin), isi());
I have the following python code using the twisted API. def function(self,filename): def results(result):
Alright so I just tried to cut down on lines of code by changing

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.