开发者

How can I optimize this C# regex? "^\s?([-*]|\d+[.])\s+"

I am not an expert at regular expression and have a regular expression

^\s?([-*]|\d+[.])\s开发者_运维问答+

I wonder if anyone has ideas about shortening/optimizing/simplifying the above regular expression?


You can save one entire character (yay!) by writing:

^\s?([-*]|\d+\.)\s+

Other than that there's not much to do here. If you don't need the contents of the alternation, you could make the group non-capturing and maybe shave a nanosecond or two off by writing

^\s?(?:[-*]|\d+\.)\s+

but that's very probably the most extreme form of premature optimization you can do. Plus, you need more characters...


As Regexes go, that one is pretty darn short and simple as it is. Does it work as you expect? If so, leave it alone.


Why would you want to optimize it? The contents of your regular expressions are already made of nonredundant atoms. You cannot simplify this more…

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜