Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用正则表达式来解析某种语法的字符串。
Pattern.compile("(\\d+)(d)(\\d+)(([\\+\\-\\*\\/])(\\d+))*"); // The regexp pattern
我希望它匹配以下字符串:
2d6 4d4+1 2d12*2-1
问题是,它还匹配以 x-*/ 结尾的字符串,例如:
3d4-
使用这个正则表达式(\d+)(d)(\d+)(([-+*/])(\d+))
(\d+)(d)(\d+)(([-+*/])(\d+))
但是 2d12x2-1 不匹配,x 在你的正则表达式中不存在,你什么也不说,因为包括x将正则表达式更改为(\d+)(d)([\dx]+)(([\+\-\*\/])(\d+))
x
(\d+)(d)([\dx]+)(([\+\-\*\/])(\d+))
编辑:
可能你需要锚?设置^并$在您的正则表达式中
^
$
Pattern.compile("^(\\d+)(d)(\\d+)(([-+*/])(\\d+))*$");