#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Texas
    Posts
    171
    Rep Power
    28

    Matching only up to the first space


    I haven't worked with these in quite some time, and I've been trying to find the proper way to match only up to the first space in a string.

    For example, if you had the string...

    This is a string.

    It would only return

    This

    Unfortunately, I am using the following...

    .*?\s

    Which will return each set up to a space, and so it returns

    This
    is
    a

    I feel as though I'm overlooking something pretty small to have it just return the first match. I appreciate any help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Start at the beginning of the string, then match everything after that that isn't a space.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Or change your code so that it only finds the first match (eg, no /g flag, PHP's preg_match and not preg_match_all, etc).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    177
    Rep Power
    234
    Originally Posted by jdavis
    I haven't worked with these in quite some time, and I've been trying to find the proper way to match only up to the first space in a string.

    For example, if you had the string...

    This is a string.

    It would only return

    This

    Unfortunately, I am using the following...

    .*?\s

    Which will return each set up to a space, and so it returns

    This
    is
    a

    I feel as though I'm overlooking something pretty small to have it just return the first match. I appreciate any help.
    you don't really need a regex in this case. ( although i know this is a regex forum). split the string on spaces/tabs, then get the first element. easy as that.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    310
    Rep Power
    162
    I know each different coding language has differences regarding regular expressions, but regex has its own code to match from the 'start of a string' and 'anything but not a space' which is ^\S+

    Would that work here?
  10. #6
  11. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Originally Posted by LDHosting
    I know each different coding language has differences regarding regular expressions, but regex has its own code to match from the 'start of a string' and 'anything but not a space' which is ^\S+

    Would that work here?
    Yeah, which is pretty much what OmegaZero said.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    310
    Rep Power
    162
    Yeah sorry I'm new to this site, the only reason I had posted that was because he left the code out

IMN logo majestic logo threadwatch logo seochat tools logo