简体   繁体   中英

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…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM