Using replace with regular expressions - VBA

三世轮回 提交于 2019-12-10 10:12:52

问题


I would like to format a text. I mean something like this: I'd like to transform this:

"something'text between apostrophes'text"

into

"something 'text between apostrophes' text"

I want to add spaces from both sides, but keep te string in '' the same. I tried to do it like this:

  Arkusz1.Cells(1, 1).Replace What:="'*'" Replacement:=" '*' "

But its result is:

"something '*' text"

Arkusz1.Cells(1,1) contains the string I want to replace. Is it possible to do this? Thanks


回答1:


You can try this regex:

('[^']+')

and replace by this:

" $1 "

Sample Source

Dim Regex As System.Text.RegularExpressions.Regex
Dim SubjectString As String = "something 'text between apostrophes' text"
Dim ResultString As String = Regex.Replace(SubjectString, "('[^']+')", " $1 ")
Console.WriteLine(ResultString)    

Regex 101 Demo



来源:https://stackoverflow.com/questions/46702510/using-replace-with-regular-expressions-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!