C#的正则表达式 [英] Regex expression for C#
问题描述
您好,
我需要使用正则表达式验证字符串,字符串如下所示,其中措辞可能不同(也是长度)但最重要的是这个。和_
Hello,
I need to validate a string using regex, string is as below where wording can be different (also in length) but the most important things are the . and _
GOOD
FW2.PLANT_DEV.APPNAME.REQUEST.*.*
FW2.PLANT_DEV.APPLICATION.REQUEST.*.*
FW2.PLANT_DEV.APPLICATION.REPLY.*.*
FW2.PLANT_PRD.APPNAME.REQUEST.*.*
FW2.PLANT_PRD.APPNAME.REQUEST.TYPE.*
FW2.PLANT_PRD.APPNAME.REQUEST.TYPE.NAME-001
FW2.PLANT_PRD.APPNAME.REQUEST.*.NAME
PLANT_DEV.APPLICATION.REQUEST.*.*
PLANT_DEV.APPLICATION.REPLY.*.*
PLANT_PRD.APPNAME.REQUEST.*.*
PLANT_PRD.APPNAME.REQUEST.TYPE.*
PLANT_PRD.APPNAME.REQUEST.TYPE.NAME
PLANT_PRD.APPNAME.REQUEST.*.NAME
NOT GOOD
FW2_SITE_DEV.APPNAME.REQUEST.*.*
FW2_PLANT_DEV.APPNAME.REQUEST.*.*
FW2.SITE.DEV.APPNAME.REQUEST.*.*
FW2.SITE_DEV.APPNAME.REQUEST
B2K_DEV.APPNAME.REQUEST.*.*
B2K.DEV.APPNAME.REQUEST.*.*
对此有任何帮助吗?
我曾经尝试过:
没有什么能比得上成功的正则表达式
Any help on this please?
What I have tried:
Nothing yet that result in a succesfull regex expression
([a-zA-Z0-9]+.{1})?\w*_{1}([^_\W]*.{1})([^_\W]*.{1})([^_\W]*.{1})(\*.{1})(\*{1})
推荐答案
这取决于你对字符串的期望:决定有效性/无效性的东西。
和一个模糊的最重要的是。并且_不是非常具体,因为单个示例包含四个'。'字符和一个'_'
但是,假设顺序并且只有第一个'。'很重要:
It depends on exactly what you expect in the string: the stuff that determines validity / invalidity.
And a vague "the most important things are the . and _" isn't very specific, given the single example contains four '.' characters and a single '_'
But, assuming order and only the first '.' is important:
^\w+?\.\w+_[\w\.\*]+
获取 Expresso 的副本[ ^ ] - 它是免费的,它会检查并生成正则表达式。
Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
NOT GOOD
FW2_SITE_DEV.APPNAME.REQUEST.*.*
FW2_PLANT_DEV.APPNAME.REQUEST.*.*
FW2.SITE.DEV.APPNAME.REQUEST.*.*
FW2.SITE_DEV.APPNAME.REQUEST
B2K_DEV.APPNAME.REQUEST.*.*
B2K.DEV.APPNAME.REQUEST.*.*
告诉我们为什么不好的原因是个好主意。
只是一些有趣的链接来帮助建设和调试RegEx。
这是al墨水到RegEx文档:
perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx的工具的链接调试它们:
.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:Learn,Build,&测试RegEx [ ^ ]
在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]
这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:
Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
这个网站也在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容:
Regexper [ ^ ]
It is a good idea to tell the reason why those are not good.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
这篇关于C#的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!