一个或多个可选参数,但至少一个。

时间:2021-10-29 08:04:08

I am writing a C# program which have many user fiels having regex validation.

我正在编写一个c#程序,其中有许多用户fiel具有regex验证。

I can not find a way to include one or many optional parameters but at least one in my regex.

我找不到一种方法来包含一个或多个可选参数,但至少在regex中包含一个。

I made the following regular expression:

我做了如下的正则表达式:

^([\" \"]*[\\+\\-][\" \"]*)?((?<leftoperand>[A-Z]+[A-Z0-9]*)[er]?[a]? *)(([\" \"]*[\\+\\-][\" \"]*)+((?<rightoperand>[A-Z]+[A-Z0-9]*)[er]?[a]? *))*[\" \"]*$

It allows me to validate a formula. This formula has specific rules, most of which I can validate:

它允许我验证一个公式。这个公式有特定的规则,大部分我都可以验证:

1. The formula can starts with a '+' or '-' sign, or white spaces
2. The formula contains only "codes" starting with at least one capital letter, then followed by the rest of the code, and can include other capital letters or numbers
3. They can be as many codes in a formula as one wishes
4. Codes ends up with optionals parameters.
      First : either a lowercase 'e' or 'r', 
      Then : an optional 'a'
      BUT it contains at least one 

I am struggling with the fourth rule...

我正在与第四条规则作斗争……

Example of correct codes:

正确的示例代码:

  • D11e
  • D11e
  • D122ea
  • D122ea
  • B9r
  • B9r
  • TOTFRra
  • TOTFRra
  • APUL2a
  • APUL2a

Example of correct formula : - D11e + D121ea - D122ra

正确公式示例:- D11e + D121ea - D122ra

Example of incorrect codes that I can manage already:

我已经可以管理的错误代码示例:

  • d122ea (starts with a lowercase character)
  • d122ea(以小写字符开始)
  • 9r (starts with a number)
  • 9r(以数字开头)
  • TOTFRer (contains 'e' and 'r')
  • 图腾(包含e和r)
  • APUL2era (contains every optional parameters)
  • APUL2era(包含所有可选参数)

Example of incorrect codes that I'd like to detect:

我想要检测的错误代码示例:

  • D11
  • 这里
  • D122
  • D122
  • ...

Those last codes don't have any of the "optional letters", but they need at least one. Possibilities are :

最后的代码没有任何“可选字母”,但它们至少需要一个。可能是:

  • [Code]e
  • e(代码)
  • [Code]ea
  • ea(代码)
  • [Code]r
  • r(代码)
  • [Code]ra
  • 风湿性关节炎(代码)
  • [Code]a
  • (代码)

Could you please help me?

你能帮我一下吗?

If you want to try it, you can go to https://regex101.com/

如果您想尝试它,可以访问https://regex101.com/

I have also tried to implement something like this without succes : Regex with all optional parts but at least one required

我还试图实现类似这样的东西,但没有成功:Regex,包含所有可选部分,但至少需要一个

I just can't find what I'm missing... Any help would be nice!

我就是找不到我所缺少的……任何帮助都是好的!

2 个解决方案

#1


2  

Try this regex :
^(?: *[+-]? *[A-Z][0-9b-df-qs-zA-Z]*([re])?(?(1)a?|a))+$

The 4th condition is handled by ([re])?(?(1)a?|a) which means "take an optional e or r, then if you did, take an optional a, but if you did not, take a mandatory a"

Like I said in my comment, you could list all possibilities with something like a|e|r|ea|ra, but if you add parameters, the number of possibilities will increase exponentially.

试试这个正则表达式:^(?):*(+ -)?*[a - z][0-9b-df-qs-zA-Z]*((re))?(?(1)? |))+第四条件是由美元((re))?(?(1)? |)这意味着“用一个可选的e或r,如果你做了,带一个可选的,但是如果你没有采取强制性的“就像我说我的评论,你可以列出所有的可能性,就像一个r e | | | ea | ra,但是如果你添加参数,可能性的数量将会成倍增加。

Demo here

演示

Edit : Codes containing non alphanum char were accepted.
Edit 2 : Actually [er]?a|[er] should be be better, and would not increase exponentially

编辑:包含非alphanum char的代码被接受。编辑2:实际上[er]?|[er]应该更好,不会呈指数级增长

#2


0  

This works too.

这个作品。

^(?:[ ]*[+-]?[ ]*[A-Z][A-Z0-9]*(?:[er]a?|a))+$

^(?:[]*(+ -)?[]*[a - z][A-Z0-9]*(?:(er)? |))+美元

https://regex101.com/r/IYO0Xv/1

https://regex101.com/r/IYO0Xv/1

Verbose

详细的

 ^ 
 (?:
      [ ]* [+-]? [ ]* [A-Z] [A-Z0-9]* 
      (?:
           [er] a?
        |  a
      )
 )+
 $ 

The single incorrect codes you want to detect would be something like this

您想要检测的单个错误代码应该是这样的

^[A-Z][A-Z0-9]*$

^[a - z][A-Z0-9]*美元

https://regex101.com/r/2caZBS/1

https://regex101.com/r/2caZBS/1

#1


2  

Try this regex :
^(?: *[+-]? *[A-Z][0-9b-df-qs-zA-Z]*([re])?(?(1)a?|a))+$

The 4th condition is handled by ([re])?(?(1)a?|a) which means "take an optional e or r, then if you did, take an optional a, but if you did not, take a mandatory a"

Like I said in my comment, you could list all possibilities with something like a|e|r|ea|ra, but if you add parameters, the number of possibilities will increase exponentially.

试试这个正则表达式:^(?):*(+ -)?*[a - z][0-9b-df-qs-zA-Z]*((re))?(?(1)? |))+第四条件是由美元((re))?(?(1)? |)这意味着“用一个可选的e或r,如果你做了,带一个可选的,但是如果你没有采取强制性的“就像我说我的评论,你可以列出所有的可能性,就像一个r e | | | ea | ra,但是如果你添加参数,可能性的数量将会成倍增加。

Demo here

演示

Edit : Codes containing non alphanum char were accepted.
Edit 2 : Actually [er]?a|[er] should be be better, and would not increase exponentially

编辑:包含非alphanum char的代码被接受。编辑2:实际上[er]?|[er]应该更好,不会呈指数级增长

#2


0  

This works too.

这个作品。

^(?:[ ]*[+-]?[ ]*[A-Z][A-Z0-9]*(?:[er]a?|a))+$

^(?:[]*(+ -)?[]*[a - z][A-Z0-9]*(?:(er)? |))+美元

https://regex101.com/r/IYO0Xv/1

https://regex101.com/r/IYO0Xv/1

Verbose

详细的

 ^ 
 (?:
      [ ]* [+-]? [ ]* [A-Z] [A-Z0-9]* 
      (?:
           [er] a?
        |  a
      )
 )+
 $ 

The single incorrect codes you want to detect would be something like this

您想要检测的单个错误代码应该是这样的

^[A-Z][A-Z0-9]*$

^[a - z][A-Z0-9]*美元

https://regex101.com/r/2caZBS/1

https://regex101.com/r/2caZBS/1