I am trying to determine the correct RegEx syntax to perform the following. I have line in a file in which I want to match every character before the first occurrence of white space.
so for example in the line:
123abc xyz foo bar
it is unclear to me why the following:
^.*\s
is matching up to the b in the word bar:
123abc xyz foo
It appears to me that the \s is greedy, however I am not certain how I can make it not greedy and just match 123abc I have tried various forms of this regex in an attempt to make it non-greedy ^.*\s? or something like this, however I have been unsuccessful. Thank you in advance
That is because
.can be any character, including space. You can tryor
instead.
That is a greedy re. But you can make non-greedy re also:
You mistake is that you have placed
?on a wrong place.Examples:
And non-greedy search with perl: