Mr. MonoChrome
Mr. MonoChrome

Reputation: 1383

match first space on a line using sublime text and regular expressions

So regular expressions have always been tough for me. Im getting frustrated trying to find a regular expression that will select the first white space on a line. So then i can use sublime text to replace that with a /

If you could give a quick explanation that would help to

Upvotes: 28

Views: 64784

Answers (2)

edi_allen
edi_allen

Reputation: 1872

You can use this regex.

^([^\s]*)\s

Upvotes: 7

Matt Tenenbaum
Matt Tenenbaum

Reputation: 1321

In the spirit of @edi's answer, but with some explanation of what's happening. Match the beginning of the line with ^, then look for a sequence of characters that are not whitespace with [^\s]* or \S* (the former may work in more editors, libraries, etc than the latter), then find the first whitespace character with \s. Putting these together, you have

^[^\s]*\s

You may want to group the non-whitespace and whitespace parts, so you can do the replacement you're talking about:

^([^\s]*)(\s)

Then the replacement pattern is just \1/

Upvotes: 58

Related Questions