Jump to content

How to match everything until the next space


Recommended Posts

"~(.+?) ~";

 

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~";

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.