-1

I've got the following text:

get close to,be next to,follow in sequence or along designated route/ direction

How would I match the space after the "e" in close and then replace it with a tab?

Although this may be easy to all of you, I've spent 4 hours trying to do this but haven't been successful.

The general rule would be "match only the space after the second word". I've got over 2000 unique lines which is why I need a regex.

Thank you!!

anubhava
  • 761,203
  • 64
  • 569
  • 643
user798719
  • 9,619
  • 25
  • 84
  • 123
  • It would be very easy to do this with this specific text .. is there a more general rule you need to follow? What language/regex engine? – Explosion Pills Nov 06 '14 at 19:58
  • Yes, the general rule I'd like help on is "always match just the second space in a line. Nothing more, nothing less." I've got to do this 2000 times and then replace the matched space with a tab – user798719 Nov 06 '14 at 20:10
  • Why do you want to use regex? Why not just use string functions? – anubhava Nov 06 '14 at 20:23
  • I'm in a regular text file using vim. You have to use a regex to identify the second space in a line. Don't I? – user798719 Nov 06 '14 at 20:26

3 Answers3

5

Search for: ^([^ ]+[ ]+[^ ]+)[ ]
Replace with: \1\t

From the beginning of the line, look for a pattern of non-spaces followed by spaces followed by another set of non-spaces. At the end, match a space character.
The replacement is everything leading up to the final space character, followed by a tab.

Demo: http://regex101.com/r/oS8vV7/1


You may not be able to match only the space you're replacing. That would require a lookbehind of variable length which isn't supported.

Community
  • 1
  • 1
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
1

In vi editor you can use this search/replacement:

:s/^\([[:blank:]]*\w\+[[:blank:]]\+\w\+\)[[:blank:]]\+/\1\t/

Or shorter (thanks to @PeterRincker):

:%s/\v(\w+\zs\s+){2}/\t
anubhava
  • 761,203
  • 64
  • 569
  • 643
0
s/\s//2

This can be used to search for a space (\s) and for removing the space, where the 2 represents the second space.

Jamal
  • 763
  • 7
  • 22
  • 32
Iyswarya
  • 1
  • 1