2

I have some misformatted phone numbers in a OpenOffice.org calc document which I want to fix. The misformatted ones look like this:

555 / 28 / 2910 / 187 / 1

How they should look:

555 / 2829101871

My question now is: How can I remove every slash in a cell but leave the first one?

The_Hegemon
  • 17
  • 1
  • 3

2 Answers2

2

This will select the bold part: 555 / (28 / 2910 / 187 / 1):

[^\/]+\/ (.*)

Live Demo

And if you want to select the three slash with the spaces on the right only: 555 / 28( / )2910( / )187( / )1

[^\/]+\/ [^\/]+( \/ )[^\/]+( \/ )[^\/]+( \/ )

Live Demo

CMPS
  • 7,505
  • 4
  • 26
  • 49
0

VBAScript does not support lookbehind. Neither, as far as I know, does it support replace callbacks, otherwise we could use the method in this question to "regex-match a pattern unless..." to do something like:

Dim myRegExp, replaced
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "^[^ /]* / |( / )"
replaced = myRegExp.Replace(SubjectString, 
     // some function that only replaces the match if Group 1 is set.
                            )

As far as I can see, this leaves us with a three-step solution:

  1. Replace the first one with something distinctive
  2. Replace the other ones
  3. Replace the first one again

1. Replacing the first one:

Search: ^([^ /]*) /

Replace: $1@@DISTINCTIVE@@

Some code:

Dim myRegExp, replaced
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "^([^ /]*) / "
replaced = myRegExp.Replace(SubjectString, "$1@@DISTINCTIVE@@")

2. Replacing the other ones:

Search: /, Replace: ""

3. Restoring the first one:

Search: @@DISTINCTIVE@@, Replace: /

Community
  • 1
  • 1
zx81
  • 38,175
  • 8
  • 76
  • 97