0

I have input data like: the pixels are of 13.45 mm for the

The output data after the macro should be like:

the pixels are of 13,45 mm for the in a Word document.

I'm trying the code below:

Sub RegexReplace1()

    Dim RegEx As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    On Error Resume Next

    RegEx.Global = True

    RegEx.Pattern = "([0-9]*).([0-9]*\s?mm)"

    ActiveDocument.Range = _
        RegEx.Replace(ActiveDocument.Range, ",")

End Sub

but it's replacing the entire thing with .

2 Answers 2

0

You need to use $1,$2 as replacement string instead of just , to insert your capture groups before and after.

RegEx.Replace(ActiveDocument.Range, "$1,$2")

Also, like Morgan said, you need to escape the . I'd also suggest using + instead of *, to make sure you actually match a measurement value.

([0-9]+)\.([0-9]+\s?mm)
0

First, since you need to match the ., you have to escape it with a \ (a single . means "I want to match any character"):

([0-9]*)\.([0-9]*\s?mm)

Then, since you need to replace the ., you have to use the matching parenthesis around it, and not around the numbers:

[0-9]*(\.)[0-9]*\s?mm
4
  • @user145345 You should provide some example to make the case clearer. Edit your question to make it obvious to everyone. Oct 5, 2016 at 9:14
  • modified the question , its more understandable now .
    – user145345
    Oct 5, 2016 at 9:19
  • i have tried the parenthesis around but not working.
    – user145345
    Oct 5, 2016 at 9:42
  • @user145345 Maybe you need to inspect what is captured before running the RegEx.Replace function, and paste your results. I cannot help you how to do that as I'm not a VB programmer. I can only help you on the regex part. Oct 5, 2016 at 9:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .