[Python] A game that uses regular expressions when, where, who, and what

Purpose

・ I learned regular expressions, so I'll enjoy using them for the time being.

environment

procedure

I wrote them all in one file (practice.py).

(1) Import the re module, which is a standard library for regular expressions

practice.py


import re

② Prepare a question

practice.py


question = "When? where? who? What did you do?"

③ Define the function four_ws_game

practice.py


def four_ws_game(sentence):
    words = re.findall(".*??", sentence)
    i = 0
    while i < len(words):
        answer = input(f"{words[i]}:").strip()
        if answer == "":
            print("Please do it seriously")
        else:
            sentence = sentence.replace(words[i], answer)
            i += 1
    print(sentence)

It's hard to understand, so let's disassemble it.

3-1. Use the findall function of the re module

--The findall function lists the ** matched parts ** in the sentence and passes it to the variable words. --. (Period) ** in regular expressions means any single character - \ * (asterisk) ** means repeat

practice.py


words = re.findall(".*??", sentence)

In other words, this program is at the end of the sentence (sentense) **? Words with any number of characters with (double-byte question) ** Find and list.

-**? (Half-width question) ** is necessary to extract the above words one by one. -Let's see how the contents of the list change depending on the presence or absence of? (Half-width question)

Yes

practice.py


words = re.findall(".*??", sentence)
print(words)

Execution result


['When?', 'where?', 'who?', 'What did you do?']
Nothing

practice.py


words = re.findall(".*?", sentence)
print(words)

Execution result


['When? where? who? What did you do?']

3-2. Rewrite the contents of the sentence (sentence) to the input value and print

practice.py


i = 0
while i < len(words):
    answer = input(f"{words[i]}:").strip()
    if answer == "":
        print("Please do it seriously")
    else:
        sentence = sentence.replace(words[i], answer)
        i += 1
print(sentence)

④ Set sentence as the first prepared question and execute it.

practice.py


four_ws_game(question)

Whole code

practice.py


import re

question = "When? where? who? What did you do?"

def four_ws_game(sentence):
    words = re.findall(".*??", sentence)
    i = 0
    while i < len(words):
        answer = input(f"{words[i]}:").strip()
        if answer == "":
            print("Please do it seriously")
        else:
            sentence = sentence.replace(words[i], answer)
            i += 1
    print("What happened? :" + sentence)

four_ws_game(question)
When?:Yesterday
where?:At home
who?:Cat
What did you do?:I fell asleep
What happened? : The cat fell at home yesterday

--I get angry when I try to leave it blank

When?:Yesterday
where?:
Please do it seriously
where?:At home
who?:Cat
What did you do?:
Please do it seriously
What did you do?:Became a dog
What happened? : The cat became a dog at home yesterday

Recommended Posts

[Python] A game that uses regular expressions when, where, who, and what
About Python and regular expressions
When using regular expressions in Python
A memo that handles double-byte double quotes in Python regular expressions
Regular expressions that are easy and solid to learn in Python
A python regular expression, str and unicode that are sober and addictive
Overlapping regular expressions in Python and Java
[Python] Regular Expressions Regular Expressions
Makes you think that Python regular expressions are great
Get the matched string with a regular expression and reuse it when replacing on Python3
Get rid of dirty data with Python and regular expressions
Precautions that must be understood when building a PYTHON environment
What I learned and coded for a function that opens a special Windows folder in Python3 ctypes
[Python] A function that searches the entire string with a regular expression and retrieves all matching strings.
When you want to replace multiple characters in a string without using regular expressions in python3 series
Use regular expressions in Python
[Python] Python and security-① What is Python?
What is a python map?
[Python3] I made a decorator that declares undefined functions and methods.
Build a game leaderboard on Alibaba cloud using Python and Redis
When creating an environment that uses python django on Ubuntu 12.04 LTS
A quick guide to PyFlink that combines Apache Flink and Python
A python script that deletes ._DS_Store and ._ * files created on Mac
[Python] I made a function that can also use regular expressions that replace character strings all at once.