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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression help 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

Can somone please help with a pattern I need to match ...

[# * #] where * means anything and it could occur anywhere and potentially more than once in the string.


So for example say I have a string "This is some text [# Anything #] , in a string, replace [# Anything #] more text." and i want to use 'Replaced' as the replacement string, so I end up with...

"This is some text Replaced , in a string, replace Replaced more text."

And if it was this...

"This is some text [# could be something else #] , in a string, replace [# could be something else #] more text."

you'd get

"This is some text Replaced , in a string, replace Replaced more text."

So the pattern is open square bracket, hash, space, wildcard (*), space, hash, closed square bracket

That is anywhere in a string , 1 or more times.

I have this code...
Code:
' replace text using regex pattern
Public Function ReplaceText(ByVal sText As String, ByVal sReplacement As String, ByVal sPattern As String) As String
  
    Dim RegEx As RegExp
    
    Set RegEx = New RegExp
    
    With RegEx
        .Global = True
        .IgnoreCase = True
        .Multiline = True
        .Pattern = sPattern
    End With
    
    ReplaceText = RegEx.Replace(sText, sReplacement)
    Set RegEx = Nothing

End Function

But am unsure where to start with the pattern syntax, so all help appreciated.

Regards,

1DMF.





"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
I'd use this pattern:
"\[# [^#]* #\]"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You're a genius PHV, thank you so much, works like a charm :)

so does that break down as follows ..

\ = escape open square bracket

[# = the start marker

[^#]* = begining with hash then anything wildcard

#\] = end marker escaping closed square bracket

is that correct?

1DMF.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
[^#]* = anything but an hash

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top