将任意字符串转换为可读的掷骰结果的最佳方法是什么?
What's the best way to convert an arbitrary string into a readable dice roll result?
我对 python 编码很陌生。
我编写的代码只保留字符“0-9”、“+”、“-”、“*”、“/”和“d”。
示例输入为 "3d6 + 4d3 + 3d2 - 433 seffesfes"
这将解析为 "3d6+4d3+3d2-433"。我被告知使用 eval() 函数是个坏主意,所以我不确定执行最终计算的最佳方法是什么。
我考虑过遍历字符串,直到找到非数字的东西,然后倒退找到前半部分,然后是后半部分,然后根据找到的内容执行函数 - 但这似乎不是对。
使用正则表达式,我们可以消除所有与您接受的字符列表不匹配的字符并删除您的字符串:
import re
re.sub("[^\d+\-*\/d]", '', your_str)
示例:
import re
re.sub("[^\d+\-*\/d]", '', "3d6 + 4d3 + 3d2 - 433 seffesfes")
# >>> '3d6+4d3+3d2-433'
正则表达式解释:
Match a single character not present in the list below [^\d+\-*\/d]:
\d matches a digit (equal to [0-9])
+ matches the character + literally (case sensitive)
\- matches the character - literally (case sensitive)
* matches the character * literally (case sensitive)
\/ matches the character / literally (case sensitive)
d matches the character d literally (case sensitive)
您可以在此处了解有关正则表达式(并使用该正则表达式)的更多信息:https://regex101.com
编辑:
在我们解析了您的初始字符串之后,要对随机值进行适当的评估,您应该使用 re.sub
:
import re
import random
def roll(match):
a,b = match.group(1).split('d')
return str(random.randint(int(a), int(a)*int(b)))
re.sub('(\d+d\d+)', roll, your_parsed_string)
解释:
re.sub
是一个函数,它接收正则表达式模式以在给定字符串中查找,然后将其替换为替代值。但是此函数还支持从回调中进行替换,即传递给 re.sub
.
的 roll
函数
回调函数接收 re.Match
对象作为参数,因此可以访问其所有特征。其中之一是 group
方法,它让我们访问由我们的正则表达式解析的组(正则表达式组由 ()
标记,让我们轻松访问我们捕获的正则表达式中的部分)。
因此,我们捕获掷骰子的每一次出现,将其作为正则表达式匹配发送到回调函数 roll
,然后将该字符串值解析为您希望实现的“掷骰子”。
例如:
your_parsed_string = '3d6+4d3+3d2-433'
re.sub('(\d+d\d+)', roll, your_parsed_string)
# >>> '8+9+2-433'
现在,要安全地评估最终字符串,请使用类似以下内容:https://github.com/pydata/numexpr
或者——自己写一个简单的解析函数
您可以使用 re.sub
中的替换功能将您的骰子符号转换为数字。
def convertDice(matchobj):
faceCount = int(matchobj.group('faces'))
return str(sum(random.randint(1, faceCount) for _ in int(matchobj.group('times')) ))
re.sub(r"(?P<times>\d+)d(?P<faces>\d+)", convertDice, your_str)
现在你有一个可以计算的字符串
我对 python 编码很陌生。
我编写的代码只保留字符“0-9”、“+”、“-”、“*”、“/”和“d”。
示例输入为 "3d6 + 4d3 + 3d2 - 433 seffesfes"
这将解析为 "3d6+4d3+3d2-433"。我被告知使用 eval() 函数是个坏主意,所以我不确定执行最终计算的最佳方法是什么。
我考虑过遍历字符串,直到找到非数字的东西,然后倒退找到前半部分,然后是后半部分,然后根据找到的内容执行函数 - 但这似乎不是对。
使用正则表达式,我们可以消除所有与您接受的字符列表不匹配的字符并删除您的字符串:
import re
re.sub("[^\d+\-*\/d]", '', your_str)
示例:
import re
re.sub("[^\d+\-*\/d]", '', "3d6 + 4d3 + 3d2 - 433 seffesfes")
# >>> '3d6+4d3+3d2-433'
正则表达式解释:
Match a single character not present in the list below [^\d+\-*\/d]:
\d matches a digit (equal to [0-9])
+ matches the character + literally (case sensitive)
\- matches the character - literally (case sensitive)
* matches the character * literally (case sensitive)
\/ matches the character / literally (case sensitive)
d matches the character d literally (case sensitive)
您可以在此处了解有关正则表达式(并使用该正则表达式)的更多信息:https://regex101.com
编辑:
在我们解析了您的初始字符串之后,要对随机值进行适当的评估,您应该使用 re.sub
:
import re
import random
def roll(match):
a,b = match.group(1).split('d')
return str(random.randint(int(a), int(a)*int(b)))
re.sub('(\d+d\d+)', roll, your_parsed_string)
解释:
re.sub
是一个函数,它接收正则表达式模式以在给定字符串中查找,然后将其替换为替代值。但是此函数还支持从回调中进行替换,即传递给 re.sub
.
roll
函数
回调函数接收 re.Match
对象作为参数,因此可以访问其所有特征。其中之一是 group
方法,它让我们访问由我们的正则表达式解析的组(正则表达式组由 ()
标记,让我们轻松访问我们捕获的正则表达式中的部分)。
因此,我们捕获掷骰子的每一次出现,将其作为正则表达式匹配发送到回调函数 roll
,然后将该字符串值解析为您希望实现的“掷骰子”。
例如:
your_parsed_string = '3d6+4d3+3d2-433'
re.sub('(\d+d\d+)', roll, your_parsed_string)
# >>> '8+9+2-433'
现在,要安全地评估最终字符串,请使用类似以下内容:https://github.com/pydata/numexpr
或者——自己写一个简单的解析函数
您可以使用 re.sub
中的替换功能将您的骰子符号转换为数字。
def convertDice(matchobj):
faceCount = int(matchobj.group('faces'))
return str(sum(random.randint(1, faceCount) for _ in int(matchobj.group('times')) ))
re.sub(r"(?P<times>\d+)d(?P<faces>\d+)", convertDice, your_str)
现在你有一个可以计算的字符串