everisk Posted September 27, 2009 Share Posted September 27, 2009 Hi, I'd like to match any characters until running into a space. currently i have (.)+ but I dont know how to mark the end. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/175684-how-to-match-everything-until-the-next-space/ Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 (.*)\s ??? Quote Link to comment https://forums.phpfreaks.com/topic/175684-how-to-match-everything-until-the-next-space/#findComment-925796 Share on other sites More sharing options...
Garethp Posted September 27, 2009 Share Posted September 27, 2009 "~(.+?) ~"; You need the ? so that it doesn't keep going until the last space. You simply put a space after the pattern. If you use (.)+ then it'll only the last character before the space. If you use (.+?) then it'll store all the characters until the next space. If you want to match whitespace (newlines and tabs along with spaces) then use \s instead of a space, like so "~(.+?)\s~"; Quote Link to comment https://forums.phpfreaks.com/topic/175684-how-to-match-everything-until-the-next-space/#findComment-925797 Share on other sites More sharing options...
everisk Posted September 27, 2009 Author Share Posted September 27, 2009 Thanks!, it's working fine Quote Link to comment https://forums.phpfreaks.com/topic/175684-how-to-match-everything-until-the-next-space/#findComment-925810 Share on other sites More sharing options...
nrg_alpha Posted September 27, 2009 Share Posted September 27, 2009 You could also use: ([^\s]+)\s -or- (\S+)\s The first example is a negated character class that basically says, 'Capture anything that is not a whitespace character [^\s] one or more times + followed by a whitespace character \s (which is not part of the capture).' , and the second one is: 'Capture any non-whitespace character \S one or more times + (followed by a whitespace character \s, which again is not part of the capture).' Different strokes for different folks. Quote Link to comment https://forums.phpfreaks.com/topic/175684-how-to-match-everything-until-the-next-space/#findComment-925829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.