英文:
How to check is %s four-digit?
问题
我有这个列表:
```python
remasterNames = [
' - Remastered',
' - %s Remaster',
' - %s Remastered',
' - Remaster %s',
' - Remastered %s',
' - Digital Remaster %s',
' - %s Digitally Remaster',
' - Digital Remastered %s',
' - %s Digitally Remastered',
' - %s - Remastered',
]
然后一个变量像 "- 2003 Remaster"
将会与列表中的每个位置进行比较。我想让 Python 检查 %s
(例如我之前提供的变量中的 2003
)是否是一个四位数,从 1984 年到 2023 年。我想将其存储在一个变量中。我可以怎么做?
SongName = "- 2023 Remastered"
# SongName 是否与 RemasterNames 中的任何选项匹配?
# "2023" 是否适合于任何这些选项(例如:" - 2023 Remastered" == " - (四位数字) Remastered")
<details>
<summary>英文:</summary>
I have this list:
remasterNames = [
' - Remastered'
' - %s Remaster'
' - %s Remastered'
' - Remaster %s'
' - Remastered %s'
' - Digital Remaster %s'
' - %s Digitally Remaster'
' - Digital Remastered %s'
' - %s Digitally Remastered'
' - %s - Remastered'
]
Then one variable like `"- 2003 Remaster"` will be compared to each position in the list. I want python to check if `%s` (for example `2003` in the variable I provided before) is a four digit number, from 1984 to 2023. And I want to store it in a variable. What can I do?
SongName = " - 2023 Remastered"
#does SongName match any of the RemasterNames options?
#does "2023" in SongName fit in any of this options (example: "- 2023 Remastered" == "- (4-digit) Remastered")
</details>
# 答案1
**得分**: 1
这是使用正则表达式的解决方案的部分代码:
```python
import re
remasterPatterns = [
r' \- Remastered',
r' \- (\d{4}) Remaster',
r' \- (\d{4}) Remastered',
r' \- Remaster (\d{4})',
r' \- Remastered (\d{4})',
r' \- Digital Remaster (\d{4})',
r' \- (\d{4}) Digitally Remaster',
r' \- Digital Remastered (\d{4})',
r' \- (\d{4}) Digitally Remastered',
r' \- (\d{4}) - Remastered'
]
name_to_test = " - 2022 Remastered"
number = 0
for pattern in remasterPatterns:
is_matched = re.match(pattern, name_to_test)
if is_matched:
number = is_matched.groups()[0]
break
print(number)
希望这有帮助。
英文:
Here is a solution using regular expressions:
import re
remasterPatterns = [
r' \- Remastered',
r' \- (\d{4}) Remaster',
r' \- (\d{4}) Remastered',
r' \- Remaster (\d{4})',
r' \- Remastered (\d{4})',
r' \- Digital Remaster (\d{4})',
r' \- (\d{4}) Digitally Remaster',
r' \- Digital Remastered (\d{4})',
r' \- (\d{4}) Digitally Remastered',
r' \- (\d{4}) - Remastered'
]
name_to_test = " - 2022 Remastered"
number = 0
for pattern in remasterPatterns:
is_matched = re.match(pattern, name_to_test)
if is_matched:
number = is_matched.groups()[0]
break
print(number)
答案2
得分: 0
为了检查RemasterNames列表中的%s占位符是否与特定范围内的四位数字匹配,您可以在Python中使用正则表达式。以下是如何完成此操作的示例:
import re
remasterNames = [
' - Remastered',
' - %s Remaster',
' - %s Remastered',
' - Remaster %s',
' - Remastered %s',
' - Digital Remaster %s',
' - %s Digitally Remaster',
' - Digital Remastered %s',
' - %s Digitally Remastered',
' - %s - Remastered'
]
SongName = " - 2023 Remastered"
for remaster in remasterNames:
pattern = re.sub(r'%s', r'\d{4}', remaster) # 使用四位数字模式替换%s
if re.match(pattern, SongName):
match = re.search(r'\d{4}', SongName) # 从SongName中提取匹配的四位数字
year = match.group() # 获取匹配的年份
print(f"The song name '{SongName}' matches the pattern '{remaster}' with the year '{year}'.")
break
else:
print("No matching pattern found.")
在这段代码中,我们遍历remasterNames列表中的每个项目,并通过将%s替换为\d{4}(匹配任何四位数字)来动态生成正则表达式模式。然后,我们使用re.match()来检查SongName是否与模式匹配。如果找到匹配项,我们使用re.search()提取匹配的四位年份,并将其存储在year变量中。
请注意,正则表达式模式假定四位数字将在SongName中精确出现一次,并且将位于remaster模式内部。如果您的要求与此示例不同,您可以修改正则表达式模式以适应您的具体要求。
英文:
To check if the %s placeholder in the RemasterNames list matches a four-digit number within a specific range, you can use regular expressions in Python. Here's an example of how you can accomplish this:
import re
remasterNames = [
' - Remastered',
' - %s Remaster',
' - %s Remastered',
' - Remaster %s',
' - Remastered %s',
' - Digital Remaster %s',
' - %s Digitally Remaster',
' - Digital Remastered %s',
' - %s Digitally Remastered',
' - %s - Remastered'
]
SongName = " - 2023 Remastered"
for remaster in remasterNames:
pattern = re.sub(r'%s', r'\d{4}', remaster) # Replace %s with a four-digit number pattern
if re.match(pattern, SongName):
match = re.search(r'\d{4}', SongName) # Extract the four-digit number from SongName
year = match.group() # Retrieve the matched year
print(f"The song name '{SongName}' matches the pattern '{remaster}' with the year '{year}'.")
break
else:
print("No matching pattern found.")
In this code, we iterate over each item in the remasterNames list and dynamically generate a regular expression pattern by replacing %s with \d{4} (which matches any four-digit number). We then use re.match() to check if the SongName matches the pattern. If a match is found, we extract the matched four-digit year using re.search() and store it in the year variable.
Note that the regular expression pattern assumes the four-digit number will appear exactly once in the SongName and that it will be located within the remaster pattern. You can modify the regular expression pattern to fit your specific requirements if they differ from this example.
答案3
得分: 0
你可以使用 split()
函数拆分字符串,并检查字符的长度 (len()
) 和是否为数字 (isnumeric()
):
import datetime
def main():
# 询问用户输入年份。不清楚你如何使用 remasterNames 列表 ???
year_str = "not_quit"
while year_str != "quit":
year_str = input("- [yyyy] Remastered? or [quit] ")
check_user_input(year_str)
if year_str == "quit":
print("end!")
remastered = "quit"
def check_user_input(input):
# 检查用户输入的值
try:
# 将其转换为整数
year_str = int(input)
print("[yyyy] is an integer number. Number =", year_str)
SongName = f" - {year_str} Remastered!"
check(SongName)
except ValueError:
try:
# 将其转换为浮点数
val = float(input)
print("[yyyy.] is a float number. Number =", val)
except ValueError:
print("[yyyy] input is not a number. It's a string")
def check(SongName):
# 检查有效年份范围 1984 - 当前年份
today = datetime.date.today()
current_year = today.year
song_l = SongName.split()
for year in song_l:
if year.isnumeric() and len(year) == 4:
distr = int(year)
print("Position in song_list:", song_l.index(year), "->", year, "found")
if distr in range(1984, current_year + 1, 1):
print("Song is in valid range")
if distr > current_year:
print(f"Invalid, song is newer as {current_year}")
if distr < 1984:
print("Song released before 1984")
if __name__ == "__main__":
main()
英文:
You can split()
the string and check the character len()
and isnumeric()
:
import datetime
def main():
# Aske for user input the year. It's not clear how you wouls use the list remasterNames ???
year_str = "not_quit"
while year_str != "quit":
year_str = input("- [yyyy] Remastered? or [quit] ")
check_user_input(year_str)
if year_str == "quit":
print("end!")
remastered = "quit"
def check_user_input(input):
# Check user input() value
try:
# Convert it into integer
year_str = int(input)
print("[yyyy] is an integer number. Number = ", year_str)
SongName = f" - {year_str} Remastered!"
check(SongName)
except ValueError:
try:
# Convert it into float
val = float(input)
print("[yyyy.] is a float number. Number = ", val)
except ValueError:
print("[yyyy] input is not a number. It's a string")
def check(SongName):
# check valid year range 1984 - current year
today = datetime.date.today()
current_year = today.year
song_l = SongName.split()
for year in song_l:
if year.isnumeric() and len(year) == 4:
distr = int(year)
print("Position in song_list:", song_l.index(year),"-->",year, "found")
if distr in range(1984, current_year+1, 1):
print("Song is in valid range")
if distr > current_year:
print(f"Invalid, song is newer as {current_year}")
if distr < 1984:
print("Song released before 1984")
if __name__ == "__main__":
main()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论