2

I have an application which outputs queries in the form of copying text to the clipboard.

I've come up with parsing the text into arrays which I then populate back to excel.

the source string contains multiple instances of dates in the format "dd.mm.yyyy" I've been using find/replace on the output column to change this to "dd/mm/yyyy". I was wondering if it would be possible/faster to replace these in the string before parsing to the array by using regex.

strPattern = "(0?[1-9]|[12][0-9]|3[01])[\.](0?[1-9]|1[012])[\.]\d{4}"
strReplace = "$dd\/dd\/dddd"

    With regEx
        .Global = True
        .MultiLine = False
        .IgnoreCase = False
        .Pattern = strPattern
    End With

    If regEx.test(sString) Then
        sString = regEx.Replace(sString, strReplace)
    End If

the end result is not what I was hoping for yet. I think the format of the 'strReplace' is wrong but I don't know regex well enough to fix it. the above was achieved by (too many) hours of web searches.

Thanks for any help.

3
  • Try strReplace = "$1/$2/$3" BTW, you do not need to test the string for a match. If there is no match, the string will not be modified. Commented May 17, 2018 at 14:25
  • Returns dd/mm/$3. So close Commented May 17, 2018 at 14:31
  • Yes, because you have no capturing group around \d{4}. Use "(0?[1-9]|[12][0-9]|3[01])\.(0?[1-9]|1[012])\.(\d{4})" pattern and strReplace = "$1/$2/$3" Commented May 17, 2018 at 14:34

1 Answer 1

1

Use

strPattern = "(0?[1-9]|[12][0-9]|3[01])\.(0?[1-9]|1[012])\.(\d{4})"
strReplace = "$1/$2/$3"

With regEx
    .Global = True
    .MultiLine = False
    .IgnoreCase = False
    .Pattern = strPattern
End With

sString = regEx.Replace(sString, strReplace)

Note:

  • You can refer to a capturing group value using $n placeholder from the replacement string
  • You do not need to use [\.], it is the same as \. (or [.])
  • You do not need to test the string for a match. If there is no match, the string will not be modified.

A hint: if you want to match these strings as "whole word", use word boundaries \b:

strPattern = "\b(0?[1-9]|[12][0-9]|3[01])\.(0?[1-9]|1[012])\.(\d{4})\b"

Now, the date-like string will only be found when not enclosed with letters, digits or _ chars.

Sign up to request clarification or add additional context in comments.

1 Comment

many thanks for the quick response and helpful hints. problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.