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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:15:46+00:00 2026-05-29T07:15:46+00:00

I am doing a project in Python where I require a user to input

  • 0

I am doing a project in Python where I require a user to input text. If the text matches a format supported by the program, it will output a response that includes a user’s key word (it is a simple chat bot). The format is stored in a text file as a user input format and an answer format.

For example, the text file looks like this, with user input on the left and output on the right:

my name is <-name> | Hi there, <-name>

So if the user writes my name is johnny, I want the program to know that johnny is the <-name> variable, and then to print the response Hi there, johnny.

Some prodding me in the right direction would be great! I have never used regular expressions before and I read an article on how to use them, but unfortunately it didn’t really help me since it mainly went over how to match specific words.

  • 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-29T07:15:47+00:00Added an answer on May 29, 2026 at 7:15 am

    Here’s an example:

    import re
    
    io = [
        ('my name is (?P<name>\w+)', 'Hi there, {name}'),
    ]
    
    string = input('> ')
    for regex, output in io:
        match = re.match(regex, string)
        if match:
            print(output.format(**match.groupdict()))
            break
    

    I’ll take you through it:


    'my name is (?P<name>\w+)'
    

    (?P<name>...) stores the following part (\w+) under the name name in the match object which we’re going to use later on.


    match = re.match(regex, string)
    

    This looks for the regex in the input given. Note that re.match only matches at the beginning of the input, if you don’t want that restriction use re.search here instead.


    If it matches:

    output.format(**match.groupdict())
    

    match.groupdict returns a dictionary of keys defined by (?P<name>...) and their associated matched values. ** passes those key/values to .format, in this case Python will translate it to output.format(name='matchedname').


    To construct the io dictionary from a file do something like this:

    io = []
    with open('input.txt') as file_:
        for line in file:
            key, value = line.rsplit(' | ', 1)
            io.append(tuple(key, value))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a project in Linux at system level in Python. So that,
I'm doing a small project in Django by using Python. In that, I am
i am doing a small project in django by using python. In that project,
i doing a small project in django by using python. In that admin can
I have a small project I am doing in Python using web.py. It's a
Im doing a project with C# winforms. This project is composed by: alt text
I'm doing a project in seam that requires restful URLs. I have a view
I'm doing a Python project in Eclipse with PyDev. I have a main file
I'm writing a setup.py file for a Python project so that I can distribute
I'm quite new to python, so I'm doing my usual of going through Project

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.