I have this regex string
(hour)?|(minute)?|(second)?
which will match hourminutesecond or hourminute or hoursecond...
However, the regex also match an empty string ""
How can I exclude the empty string from the list of matches?
One quick fix here would be to add a positive lookahead which asserts that at least one character be present:
(?=.)(?:hour)?(?:minute)?(?:second)?
Note that the | in your current pattern are not doing what you think they are. The ? you place after each time term already make each term optional.
If you don't need the separated capture groups for further processing, you could match at least one of the alternatives making none of them optional using a non capture group (?:
(?:hour|minute|second)
(?=.)(hour|minute|second)?