Home > other >  How do I get the group number from the user?
How do I get the group number from the user?

Time:04-22

I want to get the group number by using input() function.

import re

text = "protocol internet python"
group_number = input("enter the group number : ")
pattern = re.finditer(r"(\w )\s(\w )\s(\w )", text)
for searching in pattern:
    print(searching.group(group_number))

CodePudding user response:

Possible solution is the following:

import re

text = "protocol internet python"
group_number = int(input("enter the group number : "))
result = [match.groups() for match in re.finditer(r"(\w )\s(\w )\s(\w )", text)]

print(result[0][group_number])

Prints

enter the group number : 0
protocol

import re

text = "protocol internet python"
group_numbers = input("enter groups numbers separated with space: ") # 0 1
group_numbers = [int(_) for _ in group_numbers.split()]

result = [match.groups() for match in re.finditer(r"(\w )\s(\w )\s(\w )", text)]

for group in group_numbers:
    print(result[0][group])

Prints

enter groups numbers separated with space: 0 1
protocol
internet
  • Related