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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:55:07+00:00 2026-06-16T10:55:07+00:00

I am working on a personal project that is designed to open a file

  • 0

I am working on a personal project that is designed to open a file specified by the user, then to take in user input and use that input as a regular expression to search the file with. The purpose of this is to gain a deeper understanding of how regular expressions work, and how to incorporate them into programs.

My problem lies in that all input the user gives me is formatted as a string. So (correct me if I’m wrong), an input of [a-z]+ will result in the search expression “[a-z]+”. This is a problem if I want r”[a-z]+” as my search expression, as putting that in as user input will give me “r”[a-z]+”” (again, correct me if I’m wrong). This will obviously not work with regex. How do I format the input so that an input of r”[a-z]+” remains r”[a-z]+”?

This is the code section in question. The textFile in the function arguments is imported from another section of the program, and is used in the regex search:

def new_search_regex(textFile):
    """Query for input, then performs RegEx() with user's input"""
    global totalSearches
    global allSearchResults

    # ask user for regular expression to be searched
    expression = raw_input("Please enter the Regular Expression to be searched: ")

    # perform initial regex search
    foundRegex = re.search(expression, textFile)

    # if Regex search successful
    if foundRegex != None:

        # Do complete regex search
        foundRegex = re.findall(expression, textFile)

        # Print result
        print "Result: " + str(foundRegex)

        # Increment global total
        totalSearches += 1

        # create object for result, store in global array
        reg_object = Reg_Search(totalSearches, expression, foundRegex)
        allSearchResults.append(reg_object)
        print "You're search number for this search is " + str(totalSearches)        # Inform user of storage location

    # if Regex search unsuccessful
    else:
        print "Search did not have any results."

    return

Note: At the end I create an object for the result, and store it in a global array.

This is also assuming for now that the user is competently entering non-system destroying regex’s. I will soon start adding in error checking though, such as using .escape on the user input. How will this affect my situation? Will it wreak havoc with the user including ” in the input?

  • 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-16T10:55:08+00:00Added an answer on June 16, 2026 at 10:55 am

    The r"..." syntax is only useful to prevent the python compiler to interpret escape sequences (\n being converted to newline character for example). Once parsed by the compiler it will just be a regular string.

    We you read input from the user with `raw_input the compiler does not perform any escape sequence interpretations. You don’t have to do anything, the string is already correctly interpreted.

    You can test this yourself like that:

    >>> x = r"[a-z]+\n"
    >>> y = raw_input("")
    [a-z]+\n
    >>> x == y
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a personal project that uses a custom config file. The
I'm working on a personal project (C# / ASP.NET) that will use LINQ to
I'm working a personal project that's going to include a home-screen widget updated with
I'm working on a personal project involving microphones in my apartment that I can
I'm working on a personal project involving map-making tools for a tabletop game that
I'm working on a personal project and part of the idea involves the user
I have a personal project I'm working on that requires Microsoft SAPI5 -- text
I'm working on a personal learning project to help me understand how to use
Noob here... :) I'm working on a small and personal project that is already
I'm working on personal project which is basically a code editor. Imagine standard File

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.