Question

I'm having a bit of trouble using parenthesis in a vim string. I just need to add a set of parenthesis around 3 digits, but I can't seem to find where I'm suppose to correctly place them. So for example; I would have to place them around a phone number such as: 2015551212.

Right now I have a strings that separates the numbers and puts a hyphen between them. For example; 201 555-1212. So I just need the parenthesis. The final result should look like: (201) 555-1212

The string I have so far is this: s/\(\d\{3}\)\(\d\{3}\)/\1 \2-/g

How might I go about doing this?

Thanks

Was it helpful?

Solution

Just add the parens around the \1 in your replacement.

s/\(\d\{3\}\)\(\d\{3\}\)/(\1) \2-/g

If you want to go in reverse, and change "(800) 555-1212" to "8005551212", you can use something like this:

s/(\(\d\d\d\))\ \(\d\d\d\)-\(\d\d\d\d\)/\1\2\3/g

Instead of the \d\d\d, you could use \d\{3\}, but that is more trouble to type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top