用于解释整数/双精度范围输入的正则表达式或解释器
我需要验证字符串是否采用以下格式 {x:y} 或 {x,y,...,z},其中 x,y,z 是整数或双精度数。 我想我要么需要一个reg-ex和/或一个口译员。 有谁知道该怎么做? 对于这样做的图书馆的建议也将不胜感激。
第二步是拥有一个可以解释如下表达式的解释器: 0.5*{0.4,0.6,0.7} -> {0.2,0.3,0.35}。我意识到最好的方法是为此定义一个解释器可以使用的语法。
谢谢, 乙
I need to verify if a string is in the following format
{x:y} or {x,y,...,z} where x,y,z are either integers or doubles.
I was thinking I either need a reg-ex and/or an interpreter.
Does anyone know how to go about this?
Also suggestion to a library that does this would be deeply appreciated.
The second step would be having an interpreter that cand interpret expressions like this:
0.5*{0.4,0.6,0.7} -> {0.2,0.3,0.35}. I realize that the optimal approach would be to define a grammer for this that the interpreter can use.
Thanks,
B
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于存在 101 种不同的双精度格式,并且 Double.TryParse() 内置于 .net 框架中。
我将使用正则表达式或仅使用 string.split 来获取包含“x,y,z”值的字符串数组,然后对每个值调用 Double.TryParse。
由于有两种可能的格式,我首先检查输入字符串是否包含“:”,然后使用单独的代码来分割每种格式,以使代码更容易理解。
Given that there are a 101 different double formats and Double.TryParse() is built into the .net framework.
I would use a regex or just string.split to get an array of strings that contains your “x,y,z” values then call Double.TryParse on each of the values.
As you have two possible formats, I would first check to see if the input string contains “:” then have separate code to split each format so as to make the code more understandable.
我自己并不擅长正则表达式,但我确实使用它作为获取它们的来源:http://www.regexlib。 com/ 该站点上还包含一个备忘单和一个正则表达式测试器,因此您可以非常轻松地自己创建它们。
希望这有帮助!
I'm not great at Regex myself but I do use this for my source of getting them: http://www.regexlib.com/ It also includes a Cheat Sheet and a Regex Tester on that site so you can create them yourself pretty easy.
Hope this helps!
第一个选项
第二个
用法
如果您想同时检查两种类型,请将正则表达式更改为:
First option
Second
Usage
If you want check both types at once change regex to: