我正准备一个菜单,通过(readline)请求用户输入。
我的问题是,是否有办法通过使用功能(readline)和(regex)的组合来限制用户输入。我知道如何构建正则表达式,但我错过了如何结合(readline)和(regex)来实现用户输入限制的知识。
对于限制,请参阅以下脚本中的注释。
#################################################
# 1. User input required structure and rules:
#
# Product-field (first text string, max 11 characters, no digits, one of these alternatives indices, stocks, fonds, commodities).
# Product-field (second text string, max 30 characters, can have mixed digits and characters).
# Product-field format of line: "first string", "second string"
#
# Resolution-field (accepts variations such as: "5min, 5 min, 5minutes, 5 minutes".
#
# from-field (accepts only format Date and POSIXct, "YYYY-MM-DD" and "YY-MM-DD"), restrict to max 10 digit including separators.
# to-field (accepts only format Date and POSIXct, "YYYY-MM-DD" and "YY-MM-DD"), restrict to max 10 digit including separators.
#
# lines.amount-field (always a digit, max 5 digits)
#################################################
# 2. Example of filled in text
# Product:sweden
# Resolution:5min
# From:2016-01-01
# To:2016-04-02
# Lines(amount):20
#################################################
product <- NULL; resolution <- NULL; from <- NULL; to <- NULL; page.size <- NULL # vectors initiated with NULL. Will carry the user input.
cat("\014") # clears screen
product <- readline (prompt="Product:") # user input: product
resolution <- readline (prompt="Resolution:") # user input: resolution
from <- readline (prompt="From:") # user input: from
to <- readline (prompt="To:") # user input: to
lines.amount <- readline (prompt="Lines(amount):") # user input: lines