For each loop issue with pattern - vbscript

I have a scenario where I'm trying to build a loop with the pattern below. So it goes through the loop and prints out the pattern at the end. My issue is the array is throwing an exception when holding the pattern
pattern
*
**
***
****
***
**
*"
I'm having issues because the array is unable to hold the pattern. How else would I be able to construct this loop
Sub Main()
pattern = Array("'","'*","'**","'***","'**","'*"")
Dim patternstyle
'iterating using For each loop.
For each item in pattern
patternstyle = patternstyle&item&vbnewline
Next
msgbox patternstyle
End Sub

The error you are receiving will be:
Microsoft VBScript compilation error: Unterminated string constant
This is because this line;
pattern = Array("'","'*","'**","'***","'**","'*"")
has an unterminated string in the last array element.
To fix the problem either remove the trailing double-quote like this;
pattern = Array("'","'*","'**","'***","'**","'*")
or escape it by doubling it so the string is still correctly terminated, like this;
pattern = Array("'","'*","'**","'***","'**","'*""")
Output (after removing trailing double-quote):
'
'*
'**
'***
'**
'*

Related

Why is awk printing chinese looking characters

I've never had anything like this in all the years I've used AWK.
I've tried both gawk and mawk.
I've cut my awk script down to
{ print }
Should just echo every line. But every other line is printed as if it is on a different code page.
Files are created by exporting from Access, thusly:
Dim oApplication
Set oApplication = CreateObject("Access.Application")
oApplication.OpenAccessProject sFileName
For Each myObj In oApplication.CurrentProject.AllForms
WScript.Echo "Exporting FORM " & myObj.FullName
oApplication.SaveAsText acForm, myObj.FullName, sExportpath & "\" & myObj.FullName & ".form"
oApplication.DoCmd.Close acForm, myObj.FullName
dctDelete.Add "FO" & myObj.FullName, acForm
Next
Resulting source files look like
Operation =1
Option =0
Begin InputTables
Name ="Fee Types"
End
Begin OutputColumns
Expression ="[Fee Types].ID"
Expression ="[Fee Types].Type"
Expression ="[Fee Types].Category"
End
Output looks like
Operation =1
਍伀瀀琀椀漀渀 㴀 ഀഀ
Begin InputTables
਍    一愀洀攀 㴀∀䘀攀攀 吀礀瀀攀猀∀ഀഀ
End
਍䈀攀最椀渀 伀甀琀瀀甀琀䌀漀氀甀洀渀猀ഀഀ
Expression ="[Fee Types].ID"
਍    䔀砀瀀爀攀猀猀椀漀渀 㴀∀嬀䘀攀攀 吀礀瀀攀猀崀⸀吀礀瀀攀∀ഀഀ
Expression ="[Fee Types].Category"
਍䔀渀搀ഀഀ
਍
Executed with
gawk.exe -f "FilterBinary.awk" input.txt > output.txt
Hi I followed your steps and I got the same output and input:
Operation =1
Option =0
Begin InputTables
Name ="Fee Types"
End
Begin OutputColumns
Expression ="[Fee Types].ID"
Expression ="[Fee Types].Type"
Expression ="[Fee Types].Category"
End
This is still the weirdest thing I've ever seen.
Printing the ordinal value of every character showed that there was a literal null between every visible character and a couple with values of 254 and 255.
No idea why that only played havoc with every other line.
But it does explain why none of my matching was working.
Obviously, the solution was to filter it and only print characters with Ord values of 13, or above 31 and below 128.

Fetching the pattern matched value from text file in VBScript [duplicate]

This question already has answers here:
Regular Expression - How to find a match within a match?
(2 answers)
Closed 3 years ago.
I have a text file which has a single line of text containing 1 of the strings:
--Result=PASS:Passed
--Result=FAIL:Failed
Am trying to fetch the value PASS or FAIL using the pattern matching concept in VBScript but till now have been able to just match the string and retrieve the entire line. Please find below the code that am using:
Dim oRE, oMatches
Set oRE = New RegExp
oRE.Pattern = "--Result=(PASS|FAIL).*"
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\tmp\resultfile.txt",1)
Dim strline
do while not objFileToRead.AtEndOfStream
strline = objFileToRead.ReadLine()
Set oMatches = oRE.Execute(strline)
For Each oMatch in oMatches
result = oMatch.Value
Next
The result that I get now is the entire matching line. Is it possible to fetch just the PASS or FAIL substring from the text file instead of the entire line?
The Match is always the entire string that matched the pattern, you are looking for the 'Groups', which you get in vbscript through SubMatches.
if oMatches.Count > 0 then
result = oMatches(0).SubMatches(0)
end if
If you use multiple braces in the pattern, you can find these here through .SubMatches(1) etc.
Btw. your pattern does not have to match the entire input string (you don't use anchors ^$ anyway), you could just use (PASS|FAIL) as pattern, or maybe =(PASS|FAIL):.

Creating a regex to check number of occurence

I want to check how many times this sequence occurs in a webpage in UFT (012345). Basically any 6 digits in between left parenthesis and right parenthesis. I want to figure out how times this (012345) occurs in the page.
What I have so far is:
Set myReg = new regexp
myReg.pattern = '(' [0-9]+ ')'
Set k = Browser("My Webpage).Page("My Index").ChildObjects(myReg)
msgbox k.count
This doesn't work. Could someone put me in the right path?
Read reference:
Regular Expression Syntax (Scripting) and
Regular Expression Object Properties and Methods (VBScript)
Next pure VBScript code snippet could help:
sText = "(123456) (123) (abcdef)" ' !!! retrieve web page source text here !!!
' instead of testing sample
Set regEx = New RegExp ' Create regular expression
regEx.Pattern = "\([0-9]{6}\)" ' Set pattern
regEx.IgnoreCase = True ' Set case sensitivity: superabundant for numbers
regEx.Global = True ' Set global applicability: all occurrences
Set oMatches = regEx.Execute(sText) ' Execute search
RegExpCountr = oMatches.Count '
msgbox RegExpCountr

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.

Why doesn't this Ruby replace regex work as expected?

Consider the following string which is a C fragment in a file:
strcat(errbuf,errbuftemp);
I want to replace errbuf (but not errbuftemp) with the prefix G-> plus errbuf. To do that successfully, I check the character after and the character before errbuf to see if it's in a list of approved characters and then I perform the replace.
I created the following Ruby file:
line = " strcat(errbuf,errbuftemp);"
item = "errbuf"
puts line.gsub(/([ \t\n\r(),\[\]]{1})#{item}([ \t\n\r(),\[\]]{1})/, "#{$1}G\->#{item}#{$2}")
Expected result:
strcat(G->errbuf,errbuftemp);
Actual result
strcatG->errbuferrbuftemp);
Basically, the matched characters before and after errbuf are not reinserted back with the replace expression.
Anyone can point out what I'm doing wrong?
Because you must use syntax gsub(/.../){"...#{$1}...#{$2}..."} or gsub(/.../,'...\1...\2...').
Here was the same problem: werid, same expression yield different value when excuting two times in irb
The problem is that the variable $1 is interpolated into the argument string before gsub is run, meaning that the previous value of $1 is what the symbol gets replaced with. You can replace the second argument with '\1 ?' to get the intended effect. (Chuck)
I think part of the problem is the use of gsub() instead of sub().
Here's two alternates:
str = 'strcat(errbuf,errbuftemp);'
str.sub(/\w+,/) { |s| 'G->' + s } # => "strcat(G->errbuf,errbuftemp);"
str.sub(/\((\w+)\b/, '(G->\1') # => "strcat(G->errbuf,errbuftemp);"

Resources