Home > Software engineering >  How to select the first space only, not including other characters
How to select the first space only, not including other characters

Time:03-01

I want to fix some formatted strings with 'find and replace' in Visual Studio Code. To do so, I have to select first spaces only in each line, not including characters.

The format goes like this :

dc932a17 3919734822 5234dce7debe.mp4
e_f943 4961243553 03be639fa8b7.mp4
9cbcc2 4365389628 e741018829d6.mp4
543419d 4639618462 d0bd72c9b737.mp4

Desired outputs look like :

dc932a17-3919734822 5234dce7debe.mp4
e_f943-4961243553 03be639fa8b7.mp4
9cbcc2-4365389628 e741018829d6.mp4
543419d-4639618462 d0bd72c9b737.mp4

So what I want to select are :

dc932a17 3919734822 5234dce7debe.mp4
|------|^These spaces
   ^Not these characters

So I made an regex like this :

^(?:([a-zA-Z0-9_] ))\s

But this selects all the characters before the first space including in it.

dc932a17 3919734822 5234dce7debe.mp4
|-------|
    ^Selected

Is there anything I got wrong?

condition: The characters' length before spaces vary. I can't use alt shift Drag selection

CodePudding user response:

You can use

Find: ^(\S )\s
Replace: $1-

See the enter image description here

Details:

  • ^ - start of a line
  • (\s*\S ) - Group 1 ($1): zero or more whitespace chars (but not line break chars here since the pattern does not contain \r or \n) and then one or more non-whitespace chars
  • \s - one or more whitespaces (except line break chars).
  • Related