Ahmad Ismail
Ahmad Ismail

Reputation: 13892

regex to remove dash from phone numbers

I have lines like this

TEL;TYPE=CELL:343-454-1212
TEL;TYPE=CELL:34345-121212
TEL;TYPE=CELL:123456789

I need to remove dashes from them using VSCode.

So far I came with:

Search With : (TEL;TYPE=CELL:\d*)-(\d*)

Replace With : $1$2

I have to search and replace multiple times (In this case two times) to get the expected output. This is mainly because I do not know how many dashes are there.

Is there any regex which I can use to accomplish, what is being done here in single go?

Upvotes: 1

Views: 964

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627126

Visual Studio Code search and replace in the current document feature (not the one to replace in files) supports regexps with variable-width lookbehind patterns, so you can use

(?<=TEL;TYPE=CELL:[\d-]*)-

See the regex demo. Details:

  • (?<=TEL;TYPE=CELL:[\d-]*) - a position that is preceded with TEL;TYPE=CELL: and then zero or more digits or hyphens
  • - - a hyphen.

enter image description here

Upvotes: 2

Inspiraller
Inspiraller

Reputation: 3806

Wrap the number in parenthesis like this so you can restore it, removing the -

-([0-9])

replace with $1

Or Just repeat this find replace, till they are all removed:

  • find: (TEL;TYPE=CELL:[^-]*)-
  • replace: $1

Upvotes: 2

nay
nay

Reputation: 1775

I tried this limited solution.only for 2 or 3 dash.

Search with: (\d+)-(\d+)(?:-(\d+))?
Replace with: $1$2$3

but in the comment that said replace dashes with empty str also a good solution.

Upvotes: 2

Related Questions