我应该创建一个复杂的RegEx来解决手头的所有案例,还是应该在多个正则表达式中打破一个复杂的RegEx?
我担心使用复杂的Regex的性能.将复杂的Regex打造成更小的简单正则表达式会更好吗?
如果您想对性能问题有一个有意义的答案,则需要对这两种情况进行基准测试。
关于可读性/可维护性,您可以用任何语言编写不可读的代码,因此可以使用正则表达式。如果您编写了一个大的代码,请确保使用x修饰符(IgnorePatternWhitespace在c#中)并使用注释来构建您的正则表达式。
从我过去的答案之一中随机选择一个示例c#:
MatchCollection result = Regex.Matches
(testingString,
@"
(?<=\$) # Ensure there is a $ before the string
[^|]* # Match any character that is not a |
(?=\|) #Till a | is ahead
"
, RegexOptions.IgnorePatternWhitespace);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1952 次 |
| 最近记录: |