In: Computer Science
Python Programming:
Using re library, and a given string recipe1 return the indications after each ingredient '@': for example if recipe1 = "@turkey baked @350degrees" print an output like: [@turkey, @350degrees]
Program:
import re
recipe1 = input()
print(re.findall(r'@[A-Za-z0-9]+',recipe1))
Explaination:
Line 1: import re
It imports the re module into your python module. re module contains the functions that supports regular expressions.
Line 2: recipe1 = input()
It takes the user input and stores it in the variable called recipe1
Line 3: print(re.findall(r'@[A-Za-z0-9]+',recipe1))
It prints the result.
re.findall(regex,string,flags) is a function which accepts
maximum of 3 arguments. It returns all the substrings in a string
which matches the provided regular expression.
regex: regular expression
string: the string on which the operations will be performed
flags: the flags (default: 0)
Here, "r" means raw string.
Explaination of regular expression:
@[A-Za-z0-9]+
This regular expression will first search for a "@" symbol in the string, after that it checks for digits or character with the symbol.