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 6745829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:15:03+00:00 2026-05-26T12:15:03+00:00

Like many other people posting questions here, I recently started programming in Python. I’m

  • 0

Like many other people posting questions here, I recently started programming in Python.
I’m faced with a problem trying to define the regular expression to extract a variable name (I have a list of variable names saved in the list) from a string.
I am parsing part of the code which I take line by line from a file.
I make a list of variables,:

>>> variable_list = ['var1', 'var2', 'var4_more', 'var3', 'var1_more']

What I want to do is to define re.compile with something that won’t say that it found two var1; I want to make an exact match. According to the example above, var should match nothing, var1 should match only the first element of the list.

I presume that the answer may be combining regex with negation of other regex, but I am not sure how to solve this problem.

OK, I have noticed that I missed one important thing. Variable list is gathered from a string, so it’s possible to have a space before the var name, or sign after.
More accurate variable_list would be something like

>>> variable_list = [' var1;', 'var1 ;', 'var1)', 'var1_more']

In this case it should recognize first 3, but not the last one as a var1.

  • 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-26T12:15:03+00:00Added an answer on May 26, 2026 at 12:15 pm

    It sounds like you just need to anchor your regex with ^ and $, unless I’m not understanding you properly:

    >>> mylist = ['var1', 'var2', 'var3_something', 'var1_text', 'var1var1']
    >>> import re
    >>> r = re.compile(r'^var1$')
    >>> matches = [item for item in mylist if r.match(item)]
    >>> print matches
    ['var1']
    

    So ^var1$ will match exactly var1, but not var1_text or var1var1. Is that what you’re after?


    I suppose one way to handle your edit would be with ^\W*var1\W*$ (where var1 is the variable name you want). The \W shorthand character class matches anything that is not in the \w class, and \w in Python is basically alphanumeric characters plus the underscore. The * means that this may be matched zero or more times. This results in:

    variable_list = [' var1;', 'var1 ;', 'var1)', 'var1_more']
    >>> r = re.compile(r'^\W*var1\W*$')
    >>> matches = [item for item in variable_list if r.match(item)]
    >>> print matches
    [' var1;', 'var1 ;', 'var1)']
    

    If you want the name of the variable without the extraneous stuff then you can capture it and extract the first capture group. Something like this, maybe (probably a bit inefficient since the regex runs twice on matched items):

    >>> r = re.compile(r'^\W*(var1)\W*$')
    >>> matches = [r.match(item).group(1) for item in variable_list if r.match(item)]
    >>> print matches
    ['var1', 'var1', 'var1']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried solving this problem by posting other related questions here that focused
In Ruby, like in many other OO programming languages, operators are overloadable. However, only
Like many other SharePoint users, I've had to create a custom list definition. After
I'd like to enable/disable some other controls based on how many items are in
Like many SO people, I'm in front of a computer almost all day. I
as so many other people I am looking for a carousel for my homepage.
here we go with the problem: I have a clown agency with many clowns
So I have a User model that :has_many other models like Documents, Videos, Posts
Like many others on this site I am considering a move to ASP.NET MVC
I, like many others, love Eclipse as my ide of choice, and because of

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.