203

I have the following line:

hshd    household   8/29/2007   LB

I want to match anything that comes before the first space (whitespace). So, in this case, I want to get back

hshd
Svante
  • 50,694
  • 11
  • 78
  • 122
Drake
  • 2,331
  • 3
  • 19
  • 17

9 Answers9

445
([^\s]+)

works

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • 31
    I would further prepend ^ to get the first word only – soulmerge Sep 09 '09 at 15:47
  • 1
    while generally correct, I think the need for `^` depends on particular language implementations or regexp. for example in Python you'd use `re.match` for this task. – SilentGhost Sep 09 '09 at 15:51
  • 6
    This matches all the words and not just the first one, [see this example](http://regexr.com?32scv). – Ryan Gates Nov 20 '12 at 15:10
  • 1
    @RyanGates deselect Global and you will see that it works as intended and expected. Refer to SilentGhost for notes on language implementations if you're still having trouble. – Volvox Apr 24 '13 at 20:53
  • 1
    It seems that if the string begins with a space, this will ignore the space and return the first word *after* the string. I would think that if the line beings with a space, it would just return an empty string. –  Oct 23 '14 at 22:48
  • 3
    Could `[^\s]+` be simplified to `\S+` ? – Jonathan Hartley Apr 04 '17 at 15:41
  • 1
    (^[^\s]+) is what you are looking for – colin rickels Apr 23 '18 at 19:46
  • Re: "works": hm, `echo "hshd household 8/29/2007 LB" | grep -P '([^\s]+)'` leads to `hshd household 8/29/2007 LB` instead of `hshd`. Any ideas? Grep version is 3.4. – pmor Aug 22 '22 at 10:44
  • Could you explain your answer – Ricoter Mar 08 '23 at 13:17
70

This should do it:

^\S*
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
15

Perhaps you could try ([^ ]+) .*, which should give you everything to the first blank in your first group.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
dsolimano
  • 8,870
  • 3
  • 48
  • 63
11

Derived from the answer of @SilentGhost I would use:

^([\S]+)

Check out this interactive regexr.com page to see the result and explanation for the suggested solution.

MaEtUgR
  • 353
  • 3
  • 8
6

for the entire line

^(\w+)\s+(\w+)\s+(\d+(?:\/\d+){2})\s+(\w+)$
w35l3y
  • 8,613
  • 3
  • 39
  • 51
4

I think, that will be good solution: /\S\w*/

2

I think, a word was created with more than one letters. My suggestion is:

[^\s\s$]{2,}
Behzad
  • 3,502
  • 4
  • 36
  • 63
1

^([^\s]+) use this it correctly matches only the first word you can test this using this link https://regex101.com/

darshan
  • 39
  • 5
  • its derived from that answer. – darshan Jul 30 '19 at 08:24
  • 1
    Then explain why you think a new answer is required, and explain any possible difference; but you will find that it is already discussed in the comments on the accepted answer. Also, the second highest voted answer is quite similar, but somewhat more elegant in that it prefers the simpler `\S` over the equivalent but clunky `[^\s]`. – tripleee Jul 30 '19 at 08:24
1

(^\S+)

This did the trick for me. (/gm)

You could test it in this

vahid tajari
  • 1,163
  • 10
  • 20