# Start by - and and by whitespace

I want to select (by regexp) only text that start by - and end by whitespace

here text:

test-hello how-are again ok


I use this regexp:

-.*\s-


but result is not correct:

The correct result is:

"-x "
"-f "
"-hello "
"-are "
"-some "
"-what "


How I can do this by regexp?

The problem is that .* matches anything, and by default it is greedy. In your case, you probably want a non-greedy search: -.*?\s-.

‘*?’, ‘+?’, ‘??’
These are “non-greedy” variants of the operators ‘*’, ‘+’ and ‘?’.
Where those operators match the largest possible substring
(consistent with matching the entire containing expression), the
non-greedy variants match the smallest possible substring
(consistent with matching the entire containing expression).

For example, the regular expression ‘c[ad]*a’ when applied to the
string ‘cdaaada’ matches the whole string; but the regular
expression ‘c[ad]*?a’, applied to that same string, matches just
‘cda’.  (The smallest possible match here for ‘[ad]*?’ that permits
the whole expression to match is ‘d’.)


-- C-hig (elisp)Regexp Special

Hence:

-.*?\s-