复杂字符串格式验证

huangapple go评论84阅读模式
英文:

Complex String format validation

问题

我正在使用Go 1.7.4版本。有没有办法验证一个字符串是否符合特定的模板。例如,这是我的字符串(请忽略IF,THEN,它们应被视为字符串的一部分):

IF NAME: A> B> C
 AND  AS: 10796
   OR DIVISION: X> Y> Z
 AND  IP : 107.211.11.0/22
   OR IP PREFIX: 66.144.0.0/16
THEN 
    #1: PASS(60%), FAIL(40%)
    #2: PASS(40%), FAIL(35%), DISTINCTION(25%)

元素将保持相同,但出现的次数可能不同。例如:A > B 而不是 A > B >C 还可能有一个额外的子句 #3 等等。AND OR 块也可以重复。有没有办法创建一个模板来验证这个结构。我们能把它转换成JSON模式吗?

英文:

I am using Go 1.7.4. Is there any way I can validate a string to follow a specific template. For example here is my string (please ignore IF , THEN those are to be considered as part of string)

IF NAME: A> B> C
 AND  AS: 10796
   OR DIVISION: X> Y> Z
 AND  IP : 107.211.11.0/22
   OR IP PREFIX: 66.144.0.0/16
THEN 
    #1: PASS(60%), FAIL(40%)
    #2: PASS(40%), FAIL(35%), DISTINCTION(25%)

the elements will be the same but the number of occurences can differ. For eg: A > B instead of A > B >C There could be one more clause #3 and so on. AND OR block could repeat too. Is there any way I can have a template to verify this structure. Can we convert this into some JSON schema?

答案1

得分: 2

你可以在Go语言中使用ebnf包。

EBNF是一种表示形式语言语法的代码。EBNF由终结符号和非终结符产生规则组成,这些规则限制了如何将终结符号组合成合法序列。终结符号的示例包括字母数字字符、标点符号和空白字符。

这是该包的文档链接:ebnf

英文:

You may use ebnf package in go

> EBNF is a code that expresses the grammar of a formal language. An
> EBNF consists of terminal symbols and non-terminal production rules
> which are the restrictions governing how terminal symbols can be
> combined into a legal sequence. Examples of terminal symbols include
> alphanumeric characters, punctuation marks, and whitespace characters.

Here is the package doc : ebnf

huangapple
  • 本文由 发表于 2017年2月9日 14:04:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/42129346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定