Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex: replace char between two chars

Status
Not open for further replies.
Jun 28, 2001
30
US
I need to replace the 'x' character with the 'y' character whenever it occurs between two 'q' characters.

How can this be done using replace and regex? Thanks in advance for your help!
 
I guess I oversimplified my problem: there can be any number of characters between 'q' and 'q', so this isn't a simple (for me) replace.

Thanks in advance for your help.
 
I apologize for hijacking the topic. I cannot now offer the best solution, may be later strongm will. Can this code satisfy your reqiurements for now?

Dim strR As String
strR = XYReplaceBetweenTwoQs("qxxxqxqqx")


Private Function XYReplaceBetweenTwoQs(ByVal pstrString As String) As String
Dim strPattern As String
Dim strTemp As String
Dim objRegEx As System.Text.RegularExpressions.Regex

strPattern = "qx{1,}q{1,}"
strTemp = objRegEx.Replace(pstrString, strPattern, "qyq")

Return objRegEx.Replace(strTemp, strPattern, "qyq")

End Function

vladk
 
Private Enum RepMethod
Serial = 1
Staggered = 2
End Enum

Private Function DemoReplace(strSource As String, strReplace As String, Optional Method As RepMethod = Staggered) As String
With CreateObject("vbscript.regexp")
.MultiLine = True
.Global = True
If Method = Serial Then
.Pattern = "(q).+?(?=q)"
DemoReplace = .Replace(strSource, "$1" & strReplace)
Else
.Pattern = "(q).+?(q)"
DemoReplace = .Replace(strSource, "$1" & strReplace & "$2")
End If
End With
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top