Ways to delimit your text in VBSCRIPT - vbscript
I have a file with the following inputs
"SMEGOLD 1312",20131127,"C","11606233E","SMX","C",20131009,170028,"SMX","70207",0,1,4699,0,469.9,"USD",0,"",0,"",0,"",0,"",0,0,0,8062696,"",0,20131009,170028,"SYSTEM","25228","","166","121328200000223",785,0,"","","","","","","","","","","","",0,0,0,"",20131009,170028,"ADVMEE"
"SMEGOLD 1312",20131127,"C","11606233E","SMX","C",20131009,170030,"SMX","70207",0,1,4699,0,469.9,"USD",0,"",0,"",0,"",0,"",0,0,0,8062697,"",0,20131009,170031,"SYSTEM","25228","","167","121328200000223",786,0,"","","","","","","","","","","","",0,0,0,"",20131009,170028,"ADVMEE"
What i would like to achieve is to only obtain the first quote text of the line. Example "SMEGOLD 1312". Then i would like to append the first 3 characters and the last 4 characters of that extracted text to the back of line.
And move to the next line to carry on with the procedure till the end of the file.
Any advise will be a great help. I tried using the objRegEx.Pattern but to no avail
Thanks
Use a pattern that cuts/groups the first field in toto (1), its first three (2) and its last four (3) characters, and all the rest (4). Then .Replace creatively:
>> s = Replace("'SMEGOLD 1312','whatever','ADVMEE'", "'", """")
>> WScript.Echo s
>> Set r = New RegExp
>> r.Pattern = "^((""[^""]{3})[^""]*?([^""]{4}""))(.*)"
>> WScript.Echo r.Replace(s,"$1$4,$2$3")
>>
"SMEGOLD 1312","whatever","ADVMEE"
"SMEGOLD 1312","whatever","ADVMEE","SME1312"
Related
Clean up lines from log file
I have a file "D:\test.log" that has either one of two styles. This will appear if the user is offline when the user received the message: [02:19:47] Brother Aimbot (adama900): (Saved Thu Mar 31 05:15:09 2016)This is a test line It will be like this if the user is online when the user received the message: [02:19:47] Brother Aimbot (adama900): This is a test line What I would like this to do is cut out the excess parts so it would look like this if it's either the first or second style: Brother Aimbot (adama900) This is a test line then place it into a message box. Here is my code: Sub main() filename = "D:\Test.txt" Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile(filename) LNEVAL = f.ReadLine LNENUM = 0 Do Until f.AtEndOfStream For i = 1 To LNENUMs f.ReadLine Next If InStr(LNEVAL, "(S") Then LNEVAL = Left(LNEVAL, (Len("(S")+4)) MsgBox = LNEVAL End If Loop f.Close End Sub This is what I have so far.
It's fairly simple to do what you want with a regular expression replacement. Basically what you want to do is remove three things from each line: a substring between square brackets from the beginning of the string, the colon separating the name from the message, and an optional substring between parentheses after that colon. A regular expression ^\[.*?\] matches an opening square bracket at the beginning of a string and the shortest number of characters up to a closing square bracket. A regular expression \(Saved.*?\) matches an opening parenthesis followed by the word Saved and the shortest number of characters up to a closing parenthesis. However, since this part is optional you need to indicate that the expression can occur zero or one time by putting it in a non-capturing group and appending the ? modifier to it ((?:...)?). Put the submatches that you do want to preserve in parentheses to create capturing groups ^\[.*?\] (.*?): (?:\(Saved.*?\))?(.*) and replace each matching line with just the captured groups: Set re = New RegExp re.Pattern = ... Set f = fso.OpenTextFile(filename) Do Until f.AtEndOfStream MsgBox re.Replace(f.ReadLine, "$1 $2") Loop f.Close Some comments on your existing code: For i = 1 To LNENUMs: this loop is always skipped over, because you set LNEUMs to 0. Since you only do f.ReadLine inside that For loop your outer Do loop becomes an infinite loop, since you never read the file to the end. Len("(S")+4 always evaluates to 6, because the length of the string (S is not going to change, so you could just replace the expression with the numeric value. MsgBox = LNEVAL: The MsgBox function doesn't work that way. Remove the = between function name and message.
Is there a way to make special characters work when using InStr in VBScript?
A VBScript is in use to shorten the system path by replacing entries with the 8.3 versions since it gets cluttered with how much software is installed on our builds. I'm currently adding the ability to remove duplicates, but it's not working correctly. Here is the relevant portion of code: original = "apple;orange;apple;lemon\banana;lemon\banana" shortArray=Split(original, ";") shortened = shortArray(1) & ";" For n=2 to Ubound(shortArray) 'If the shortArray element is not in in the shortened string, add it If NOT (InStr(1, shortened, shortArray(n), 1)) THEN shortened = shortened & ";" & shortArray(n) ELSE 'If it already exists in the string, ignore the element shortened=shortened End If Next (Normally "original" is the system path, I'm just using fruit names to test...) The output should be something like apple;orange;lemon\banana The issue is entries with punctuation, such as lemon\banana, seem to be skipped(?). I've tested it with other punctuation marks, still skips over it. Which is an issue, seeing how the system path has punctuation in every entry. I know the basic structure works, since there are only one of each entry without punctuation. However, the real output is something like apple;orange;lemon\banana;lemon\banana I thought maybe it was just a character escape issue. But no. It still will not do anything with entries containing punctuation. Is there something I am doing wrong, or is this just a "feature" of VBScript? Thanks in advance.
This code: original = "apple;orange;apple;lemon\banana;lemon\banana" shortArray = Split(original, ";") shortened = shortArray(0) ' array indices start with 0; & ";" not here For n=1 to Ubound(shortArray) 'If the shortArray element is not in in the shortened string, add it 'i.e. If InStr() returns *number* 0; Not applied to a number will negate bitwise ' If 0 = InStr(1, shortened, shortArray(n), 1) THEN If Not CBool(InStr(1, shortened, shortArray(n), 1)) THEN ' if you insist on Not WScript.Echo "A", shortArray(n), shortened, InStr(1, shortened, shortArray(n), vbTextCompare) shortened = shortened & ";" & shortArray(n) End If Next WScript.Echo 0, original WScript.Echo 1, shortened WScript.Echo 2, Join(unique(shortArray), ";") Function unique(a) Dim d : Set d = CreateObject("Scripting.Dictionary") Dim e For Each e In a d(e) = Empty Next unique = d.Keys() End Function output: 0 apple;orange;apple;lemon\banana;lemon\banana 1 apple;orange;lemon\banana 2 apple;orange;lemon\banana demonstrates/explains your errors (indices, Not) and shows how to use the proper tool for uniqueness (dictionary).
How can I strip all numbers from a string
I would I go about stripping all numbers from a string? This code below is what I have been trying. objRegExp.Pattern = "((?![a-zA-Z]).)+" queryForHTML = objRegExp.Replace(queryForHTML, "")
KISS. You want to find all sequences of one or more digits (to delete them). So the pattern is "\d+". Trying to specify what you don't want to find/delete is errorprone/clumsy/not necessary. >> Set re = New RegExp >> re.Pattern = "\d+" >> re.Global = True >> WScript.Echo re.Replace("a1b234c", "") >> abc >>
This works in JavaScript /[0-9]/g it will find all occurrences of digits, and your replace will trim those out. /g is for global, I think vbscript has a Global property which you can set
vbscript - Replace all spaces
I have 6400+ records which I am looping through. For each of these: I check that the address is valid by testing it against something similar to what the Post Office uses (find address). I need to double check that the postcode I have pulled back matches. The only problem is that the postcode may have been inputted in a number of different formats for example: OP6 6YH OP66YH OP6 6YH. If Replace(strPostcode," ","") = Replace(xmlAddress.selectSingleNode("//postcode").text," ","") Then I want to remove all spaces from the string. If I do the Replace above, it removes the space for the first example but leave one for the third. I know that I can remove these using a loop statement, but believe this will make the script run really slow as it will have to loop through 6400+ records to remove the spaces. Is there another way?
I didn't realise you had to add -1 to remove all spaces Replace(strPostcode," ","",1,-1)
Personally I've just done a loop like this: Dim sLast Do sLast = strPostcode strPostcode = Replace(strPostcode, " ", "") If sLast = strPostcode Then Exit Do Loop However you may want to use a regular expression replace instead: Dim re : Set re = New RegExp re.Global = True re.Pattern = " +" ' Match one or more spaces WScript.Echo re.Replace("OP6 6YH.", "") WScript.Echo re.Replace("OP6 6YH.", "") WScript.Echo re.Replace("O P 6 6 Y H.", "") Set re = Nothing The output of the latter is: D:\Development>cscript replace.vbs OP66YH. OP66YH. OP66YH. D:\Development>
This is the syntax Replace(expression, find, replacewith[, start[, count[, compare]]]) it will default to -1 for count and 1 for start. May be some dll is corrupt changing the defaults of Replace function.
String.Join("", YourString.Split({" "}, StringSplitOptions.RemoveEmptyEntries)) Because you get all strings without spaces and you join them with separator "".
VB6 getting ride of large spaces
Hey all, i am trying to replace large spaces between text with just one. My output looks like this right now: 5964215"> This is just the first example of the spaces 5964478"> This would be the 2nd example of showing how many spaces this thing has in each sentence. 5964494"> That comes from a textbox that has multi-line to true. Here is what it looks like when it doesn't have multi-line to true. http://www.june3rdsoftware.com/forums/vb6.jpg I can not seem to get the spaces to go away! BTW, this text is from a webpage if that makes any difference. David
According to the suggestion of MvanGeest, here is some VB code to replace blocks of white spaces: Sub test() Dim x As String, y As String x = "abcd defg 1233" Dim re As New RegExp re.Pattern = "\s+" re.Global = True y = re.Replace(x, " ") Debug.Print y End Sub To make this work, you will have to add a reference to "Microsoft VBScript Regular Expresssions" to your project.
Assuming no regex support, why not set up a simple state machine that will set the state=1 when a space is found and set state=0 once a non-space is encountered. You can move char by char when state=0 (thus copying over only 1 space per series of spaces).
Also assuming no regex, you could try something like str = "long text with spaces " i = LenB(str) str = Replace(str, " ", " ") Do While LenB(str) <> i i = LenB(str) str = Replace(str, " ", " ") Loop Of course this code could be optimized for long space sequences but it might be all you need as well