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

I have a string object that begins

Status
Not open for further replies.

deb18

Programmer
May 19, 2003
40
0
0
US
I have a string object that begins with the text:

"DESCRIPTION (assorted text here) text continued..."

I would like to remove from the string all text from the word "DESCRIPTION" through the end of the parentheses.
I tried

Dim pattern As String = "DESCRIPTION(.*)"
Dim re As New Regex(pattern)
re.Replace(testtxt, "")

where texttxt is the original string and this does not seem to work.

What is the best way to do this?

Thanks!
 
Dim poslpara as integer
Dim posrpara as integer


poslpara=Instr(texttxt,"(")
posrpara =Instr(texttxt,")")

texttxt=Left(texttxt,poslpara) & Mid(texttxt,posrpara)

 
Dim poslpara , posrpara

poslpara=Instr(texttxt,"(")
posrpara =Instr(texttxt,")")

texttxt=Left(texttxt,poslpara) & Mid(texttxt,posrpara)

 
2nd one is correct-i forgot it is vbscript and not vb
 
Look out for Instr returning 0. Also you do not want to include the parens, right?

Dim poslpara , posrpara

poslpara=Instr(texttxt,"(")
if poslpara > 0 then
posrpara = Instr(poslpara + 1, texttxt,")")
if posRpara > 0 then
texttxt = Left(texttxt,poslpara - 1) & Mid(texttxt,posrpara + 1)
else
texttxt = Left(texttxt,poslpara - 1)
End if
End if


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Isn't there a way to do this in a more refined fashion using RegEx's?

...the problem is I don't know how! Can anyone else help, please?

Lazer [blush]
 
Ms. Deb,

A RegExExpert just pointed me to the answer. It may be found at and was posted by Dave Greene

- Lazer





Globally replace text using just 5 lines of Visual Basic code

If you've ever tried to implement a text search and replace function in Visual Basic, you probably resorted to lengthy parsing loops that used Instr() and an entire host of string functions. If so, you'll be glad to know that there's a much easier way.

With the advent of VBScript 5.0, Microsoft introduced the Regular Expression engine, which you can also use in Visual Basic. If you've used Perl or JavaScript, you may be familiar with these pattern-matching powerhouses. In a nutshell, regular expressions let you define a pattern, literal or representative, which you can then match against a second string. For example, the literal pattern 'abc' as a regular expression would find a match in 'dabcef', 'abcdef', or 'defabc'. To further refine a regular expression, the Regular Expression engine offers a host of special metacharacters, similar to wildcard characters. For example, using these metacharacters, you could search for any word that began with a letter and ended in a digit. To view these metacharacters and their uses, visit Microsoft Windows Script Technologies.

then search in the VBScript documentation for the RegExp object's Pattern property.

To create a simple find and replace subroutine, first download the VBScript DLL from
Once you've registered the dll on your system, in your Visual Basic project set a reference to Microsoft VBScript Regular Expressions. Now, via code, create a RegExp object, like so

Set MyReg = new RegExp

Next, set the object's properties.

MyReg.IgnoreCase = True
MyReg.Global = True
MyReg.Pattern = "abc"

Here, we've used a literal string to create a global, case-insensitive pattern. Finally, you execute the object's Replace method on the target string, as in

txtSearchText = MyReg.Replace(txtSearchText, "def")

That's *all* there is to it! This example would replace "abc" with "def" wherever it occurred within txtSeartText. To execute the replace only on the first matching string, set the Global property to False.
 
Regular Expressions work great in VBSCript, but what about VB.NET? The RegExp object is a built-in object, so I can't create it with CreateObject. I find it hard to believe that I would have to recreate this.

Zy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top