tags:

views:

61

answers:

3

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?

+1  A: 

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.

Andrew Barber
oh, ok thanks. I was just wondering if I can somehow simplify that regex so its more readable and easy to understand, whether for me, or someone else looking at it in the future
jiewmeng
Regexes are among some of the least 'readable' constructs you will find in programming, IMO. They just look strange, especially when you are not very fluent with them :P
Andrew Barber
+1 Perhaps even the idea of optimizing a regex is little skew?
cottsak
+1  A: 

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

Benoit
+1 for using the word 'atoms'. I love that word, used appropriately. Don't ask me why! (oh, and for a correct answer!)
Andrew Barber
The word “atom” coms from vim documentation on patterns. I did not invent it :)
Benoit
Oh yeah, I know; that's what I mean. :) Mostly, I'm way overdue for bed and channeling a Care Bear!
Andrew Barber
+2  A: 

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...

Tim Pietzcker
EGADS! +1 for finding the 1-char savings by removing the square brackets; hehe
Andrew Barber