Regular Expression: Match Any Word Until First Space

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
2

8 Answers

([^\s]+)

works

7

This should do it:

^\S*

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

1

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.

for the entire line

^(\w+)\s+(\w+)\s+(\d+(?:\/\d+){2})\s+(\w+)$

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

0

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

[^\s\s$]{2,}

^([^\s]+) use this it correctly matches only the first word you can test this using this link

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest