regular expression: match any word until first space

前端 未结 8 1025
情话喂你
情话喂你 2020-12-02 06:20

I have the following line:

hshd    household   8/29/2007   LB

I want to match anything that comes before the first space (whitespace). So,

相关标签:
8条回答
  • 2020-12-02 06:23

    for the entire line

    ^(\w+)\s+(\w+)\s+(\d+(?:\/\d+){2})\s+(\w+)$
    
    0 讨论(0)
  • 2020-12-02 06:26

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

    0 讨论(0)
  • 2020-12-02 06:26

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

    [^\s\s$]{2,}
    
    0 讨论(0)
  • 2020-12-02 06:36
    ([^\s]+)
    

    works

    0 讨论(0)
  • 2020-12-02 06:46

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

    0 讨论(0)
  • 2020-12-02 06:47

    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.

    0 讨论(0)
提交回复
热议问题