以下的正则表达式是什么?

发布于 2024-09-15 21:22:06 字数 1466 浏览 3 评论 0 原文

在针对 Active Directory 执行某些任务时,我需要遵守一个术语。

这是命名法:

  • TT-EEE-助记符:如果 TT = 'GA' 或 'GS' 或 'PA' 或 'PF' ->要创建的架构是一个“组”,groupScope 为全局
  • LT-EEE-助记符:如果T = 'A' 或 'G' 或 'I' 或 'N' 或 'P' ->要创建的架构是一个“组”,groupScope 为本地域
  • TTT-EEE-助记符:如果 TTT* = 'CNX' 或 'GST' 或 'SVC' ->要创建的 shema 是一个“用户”
  • T-SSSS-助记符:如果T = 'A' 或 'L' 或 'M' 或 'R' 或 'S' ->要创建的架构是“organizationUnit”

我所追求的是一种比这更简单、更有效的方法:

If(dn.Substring(3, 2).Contains("GA") _
    Or variable.Substring(3, 2).Contains("GS") _
    Or dn.Substring(3, 2).Contains("PA") _
    Or dn.Substring(3, 2).Contains("PF")) Then 
    schema = "group" ' Global'
Else If(dn.Substring(4, 1).Contains("A") _
    Or dn.Substring(4, 1).Contains("G") _
    Or dn.Substring(4, 1).Contains("I") _
    Or dn.Substring(4, 1).Contains("N") _
    Or dn.Substring(4, 1).Contains("P")) Then
    schema = "group" ' Local'
Else If(dn.Substring(3, 3).Contains("CNX") _
    ' Well... You get the idea, don't you?
End If

我想我可以使用一个正则表达式,或者可能为我得到的每个命名法使用一个,类似的东西。

在这种情况下,有没有办法让 RegularExpression 变得方便?或者最好还是坚持那个古老的大假设?欢迎任何建议。

抱歉询问,但我不习惯使用 RegularExpression。我知道它们的存在,也知道它们能做的一些事情,但仅此而已。

谢谢!

I have a nomenclature to respect while performing some tasks against the Active Directory.

Here's the nomenclature:

  • TT-EEE-Mnemonic: if TT = 'GA' or 'GS' or 'PA' or 'PF' -> the schema to create is a "group", with a groupScope of Global.
    1. LT-EEE-Mnemonic: if T = 'A' or 'G' or 'I' or 'N' or 'P' -> the schema to create is a "group", with a groupScope of Domain local.
    2. TTT-EEE-Mnemonic: if TTT* = 'CNX' or 'GST' or 'SVC' -> the shema to create is an "user"
    3. T-SSSS-Mnemonic: if T = 'A' or 'L' or 'M' or 'R' or 'S' -> the schema to create is an "organizationUnit"

What I'm after is a simpler and more effective way than this:

If(dn.Substring(3, 2).Contains("GA") _
    Or variable.Substring(3, 2).Contains("GS") _
    Or dn.Substring(3, 2).Contains("PA") _
    Or dn.Substring(3, 2).Contains("PF")) Then 
    schema = "group" ' Global'
Else If(dn.Substring(4, 1).Contains("A") _
    Or dn.Substring(4, 1).Contains("G") _
    Or dn.Substring(4, 1).Contains("I") _
    Or dn.Substring(4, 1).Contains("N") _
    Or dn.Substring(4, 1).Contains("P")) Then
    schema = "group" ' Local'
Else If(dn.Substring(3, 3).Contains("CNX") _
    ' Well... You get the idea, don't you?
End If

I guess I could use a RegularExpression, or perhaps one for each of the nomenclature I got, something alike.

Is there a way a RegularExpression could become handy in this situation? Or would it be best to stick with that old big-If? Any suggestions are welcome.

Sorry for asking, but I'm not used to use RegularExpression. I know they exist, and bit of what they can do, but that's all.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

决绝 2024-09-22 21:22:06

您的代码似乎与您的描述不符。根据您的描述,您可能需要以下正则表达式:

^(((GA|GS|PA|PF)|L[AGINP]|(CNX|GST|SVC))-EEE|[ALRMS]-SSSS)$

编辑:您可能需要阅读 本教程 关于正则表达式的含义,请特别查找“字符类”和“分组和替代项”部分。

简而言之,横线字符(即|)是“OR”运算符。方括号(即[])是字符类;换句话说,字符之间是“OR”。

Your code does not seem to conform your description. With your description, you may want the following regular expression:

^(((GA|GS|PA|PF)|L[AGINP]|(CNX|GST|SVC))-EEE|[ALRMS]-SSSS)$

EDIT: you may want to read up this tutorial about what the regular expression means, specifically look for the "Character classes" and "Grouping and alternatives" sections.

In short, the bar character (i.e. |) is the "OR" operator. The square brackets (i.e. []) are the character class; in other words, "OR" between the characters.

清欢 2024-09-22 21:22:06

它会大大减少测试和显式 Or 的数量。

If Regex.IsMatch(dn, "^CN=(G[AS]|P[AF])-") Then
    schema = "group"    ' Global                 'damn syntax highlighting
ElseIf Regex.IsMatch(dn, "^CN=L[AGINP]-") Then
    schema = "group"    ' Local                  'damn syntax highlighting
ElseIf Regex.IsMatch(dn, "^CN=(CNX|GST|SVC)-") Then
    schema = "user"
ElseIf Regex.IsMatch(dn, "^CN=[ALMRS]-") Then
    schema = "organizationUnit"
End If

It'd vastly reduce the number of tests and explicit Ors.

If Regex.IsMatch(dn, "^CN=(G[AS]|P[AF])-") Then
    schema = "group"    ' Global                 'damn syntax highlighting
ElseIf Regex.IsMatch(dn, "^CN=L[AGINP]-") Then
    schema = "group"    ' Local                  'damn syntax highlighting
ElseIf Regex.IsMatch(dn, "^CN=(CNX|GST|SVC)-") Then
    schema = "user"
ElseIf Regex.IsMatch(dn, "^CN=[ALMRS]-") Then
    schema = "organizationUnit"
End If
玩套路吗 2024-09-22 21:22:06

怎么样...

Dim Schema As String = Nothing
Select Case dn.SubString(3, 2) ' Am not sure about your index of 3 here!
  Case "GA", "GS", "PA", "PS"
    Schema = "group"
End Select

If IsNothing(Schema) Then
  Select Case ...
End If

etc.

How about...

Dim Schema As String = Nothing
Select Case dn.SubString(3, 2) ' Am not sure about your index of 3 here!
  Case "GA", "GS", "PA", "PS"
    Schema = "group"
End Select

If IsNothing(Schema) Then
  Select Case ...
End If

etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文