Jump to content

How to match everything until the next space


everisk

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.