RegEx - greedy white space match
regex
Solution
That is because `.` can be any character, including space. You can try
^[^ ]*\s
or
^\S*\s
instead.
That is a greedy re. But you can make non-greedy re also:
^.*?\s
You mistake is that you have placed `?` on a wrong place.
Examples:
$ echo aaaa bbb cccc dddd > re.txt
$ cat re.txt
aaaa bbb cccc dddd
$ egrep -o '^.*\s' re.txt
aaaa bbb cccc
$ egrep -o '^\S*\s' re.txt
aaaa
$ egrep -o '^[^ ]*\s' re.txt
aaaa
And non-greedy search with perl:
$ perl -ne 'print "$1\n" if /^(.*?)\s/' re.txt
aaaa
Problem
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