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

Replace str using wildcards? 2

Status
Not open for further replies.

nexcar

Programmer
Jun 30, 2003
49
0
0
US
I have raw html code for a news story. I've used the

Replace(MyString, ReplaceWhat, ReplaceWith) as String

function to replace all the <p> and </p> tags, but I'm having trouble with the <a href=URL> tags. See, the problem is that the 'a' tags vary in length and content so unless there is some way to use wildcards (like * and ?) in the replace function and just say:

Replace(MyString, &quot;<*>&quot;, &quot;&quot;)

To remove all html tags in the text. Here's an example of the raw html text:

Japanese-style gourmet dining -- and play gastronomic <a href=&quot; roulette.

Does anybody know how to remove this from the string data? Or if there IS a way to use wildcards in the Replace function. {SOS}



RUN FunnySignatureMessage
 
Sounds like a job for Regular Expressions. Searching the forum should find a number of examples which can get you started down the quite powerful RE path.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
YES!!!! EUREKA!! And whatever else you say when experiencing an apiphany. For anyone else who wants to know the solution, here it is:

Code:
    Dim objReg As New RegExp
    objReg.Pattern = &quot;<a*>&quot;
    txtData.Text = objReg.Replace(txtData.Text, &quot;&quot;)

You must include the Microsoft VBScript Regular Expressions reference in your project.

This will allow you to use wildcards to produce nearly the same result as the standard Replace() function.

Many Thanks for the direction Cajun!

RUN FunnySignatureMessage
 
Happy to help

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Until then, this may help you out:
Code:
Function TestRep()
    Dim TheString As String
    Dim iPos1 As Integer
    Dim iPos2 As Integer
    
    TheString = &quot;alkdjalsjefkpwak&quot;
    iPos1 = InStr(1, TheString, &quot;<&quot;)
    iPos2 = InStr(1, TheString, &quot;>&quot;)
    Do Until iPos1 = 0 Or iPos2 = 0
        TheString = Left$(TheString, iPos1 - 1) & Right$(TheString, Len(TheString) - iPos2)
        iPos1 = InStr(1, TheString, &quot;<&quot;)
        iPos2 = InStr(1, TheString, &quot;>&quot;)
    Loop
    TestRep = TheString
End Function
 
It does work, albeit lengthy, but you get a star for your effort. Thanks!

RUN FunnySignatureMessage
 

Thank you for the acknowledgement!

>albeit lengthy

Just because RE has the work code hidden from the user.

The VB code stuck into a function becomes also &quot;shorter&quot;, or easier to use for something like this, even though with RE you can use the same object and just change the pattern in order for it to adopt to a different search pattern.

Just to note:
The VB code will be alot faster on shorter strings than the regular expression with a replace pattern like:

objRegExp.Pattern = &quot;<.*?>&quot;

The RE method will produce better results against the VB code as the size of the text grows.

We, tested this (similar code), well, actually CajunCenturion originally did a while back for me, and on a very large piece of text, (56K), and I have since, and the RE won hands-down....just not on a smaller piece of text.
The breaking point where the RE is faster seems to be around 8000 chars.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top