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

Remove some characters from a string

Status
Not open for further replies.

cerruti1881

Technical User
Nov 8, 2012
10
AU
Assume there is a string: 186255W,186254L,PRDCTX001,PRDCTX012,196278L,PRDCTX146,PRDCTX241
I want a VBScript that can make the result as: 186255w,186254L,196278L
In another word, remove those start with "PRDCTX". Remember their positions are random.
 
A starting point
Code:
Set re = CreateObject("VBScript.RegExp")
re.Multiline = True
re.Global = True
re.Pattern = "(^|,)PRDCTX[\d]+"
yourString = "PRDCTX999,186255W,186254L,PRDCTX001,PRDCTX012,196278L,PRDCTX146,PRDCTX241"
yourString = re.Replace(yourString, "")
If Left(yourString, 1) = "," Then yourString = Mid(yourString, 2)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV. Someone has advised the similar way:

Dim regEx, strRegularExpression, strInitialString, strResult, colMatches, strMatch

strInitialString="186255w,186254L,PRDCTX140,PRDCTX141,196278L,PRDCTX146,PRDCTX241,"
strRegularExpression="\d{5,6}(w|W|l|L),"
Set regEx = New RegExp
regEx.Pattern = strRegularExpression
regEx.Global = True
regEx.Multiline = True
set colMatches = regEx.Execute(strInitialString)
For Each strMatch in colMatches
strResult= strResult & strMatch
Next
wscript.echo "strInitialString:" & strInitialString
wscript.echo "strResult: " & strResult
 
The listed methods are subtly different and will yield different results should "new" data appear in your list. One method narrowly defines what you want to get rid of, the other narrowly defines what you want to keep.

PHV's method eliminates all items that start with "PRDCTX", leaving all other items alone. The 2nd method eliminates everything except those items that are 5 or 6 digits followed by a single "W" or "L" (case insensitive).

The method you choose to use should be dictated by how well you know the data you have and what new data, other than what was shown in your example (if any), you expect to process.
 
PHV's solution has the advantage of meeting the specification outlined in the OP - and, IMHO, of being a better solution in general, avoiding the necessity of rebuilding the string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top