This question already has an answer here:
vbscript split string with colon delimiter
(1 answer)
Closed 1 year ago.
I am trying to come up with a method for extracting information from a file heading. The overall naming convention of the file heading will remain the same but portions of the heading will vary in character length. Below are two possible examples of such file headings:
012345678-012345-xxxx-yyyyy.txt
012345678-012345-xxx-yyyyyy.txt
Is there a way to extract values from these file headings such that it returns whatever appears between the second and third hyphen? Using the examples above it would return:
xxxx
xxx
Furthermore, is it possible to extract the values between the final hyphen and the period? Using the example above it would return:
yyyyy
yyyyyy
Extracting values is trivial when the character lengths are fixed, but I don't know if it's possible to do a similar extraction when the character lengths vary. I would normally use something like this to extract the information from a fixed-length naming convention but don't know how to adapt it to something where the character lengths change. For example, the snippet below is a function which extract the first nine characters in a file heading (in this case it would extract '012').
Function getthething(foo)
getthething = Mid(foo,1,3)
End Function
Any guidance would be very appreciated. Thank you.
You can do all of this using the Split function. Here's a wrapper function that simplifies things:
Function GetField(p_sText, p_sDelimiter, p_iIndex)
Dim arrFields
arrFields = Split(p_sText, p_sDelimiter)
If UBound(arrFields) >= (p_iIndex - 1) Then
GetField = arrFields(p_iIndex - 1)
Else
GetField = ""
End If
End Function
You can use this function like this:
Dim sFileName
Dim sYs
sFileName = GetField("012345678-012345-xxxx-yyyyy.txt", ".", 1)
sYs = GetField(sFileName, "-", 4)
MsgBox sYs
or simply:
MsgBox GetField(GetField("012345678-012345-xxxx-yyyyy.txt", ".", 1), "-", 4)
This question already has answers here:
How do you write multiline strings in Go?
(12 answers)
Closed 2 years ago.
How do you define variable value that includes new line?
const statement = "SELECT *
FROM `users` u;";
It shows error newline in string.
You would normally use a raw string literal like this
const s = `first line
second line`
However, this is not possible in your case since the string itself contains backticks. In this case I would use + to connect the lines:
const statement = "SELECT *" +
" FROM `users` u;";
The string value in this example does not contain a newline (but that's ok for SQL). If you really want a newline in the string, use the escape sequence \n.
const s = "first line\nsecond line"
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):.
This question already has answers here:
How Do I Use VBScript to Strip the First n Characters of a String?
(4 answers)
Closed 6 years ago.
I have a program that prints a string to a notepad file, the output being something random:
#'f7ruhigbergbn
I want to however remove the first 3 characters from the pasted result, how can I do this?
myString = "#'f"
result = workings - myString
This does not work, bare in mind the first 3 characters are always going to be #'f
Any thoughts? thanks
You can use:
result = Mid(workings, 4)
You can use Right function for get the X characters of the right side of string. With Len function you can get the length of the string.
Right(myString,Len(myAtring) - 3)
With this, you get a new string whitout the three first characters, now you can assign to the same string:
myString = Right(myString,Len(myAtring) - 3)
Try this:
mystring = "#'f7ruhigbergbn"
result = Mid(mystring, 3, mystring.length)
TL;DR
I need some help making a regex that will match any commas in a string that are side by side with unlimited white space around them and between them. The commas and their surrounding white space cannot be within matching single quotes or double quotes. I then need to capture the non-whitespace values from around those commas and count how many of those commas there are.
The values captured from around the commas will become their own values in the final array, while the commas that were counted will become nil values that are added to the final array.
Explanation of the problem:
This is a pretty complex problem so any help is greatly appreciated. I'm adding functionality to a library I've been using for a while now. I have this string that contains an array
"['d,og,f:asdf,:hello,",,\",,alsee',,,'ho,la', "-123,4,5.3", true, :good, false,,, "gr\'\'\'true,\',\'ee\"n", ":::testme", true]"
I would like to split this string only around select commas so that I have an array containing the following values
'd,og,f:asdf,:hello,",,\",,alsee'
nil
nil
'ho,la'
"-123,4,5.3"
true
:good
false
nil
nil
"gr\'\'\'true,\',\'ee\"n"
":::testme"
true
Then nil values are coming from the side by side commas that are not contained in any string. I wrote the following regex to split the string above (I already got rid of the start and end brackets):
/(?<=(?:['\"]|false|true|^|,)),(?=(?:\s*(?:(?::[\w]+)|(?:(?::?(?:\"[\s\S]*\")|(?:'[\s\S]*'))|(?:false|true)))\s*(?:,|$)))/
This splits the string so I get these values:
(0) "'d,og,f:asdf,:hello,",,\",,alsee',,"
(1) "'ho,la'"
(2) " "-123,4,5.3""
(3) " true"
(4) " :good, false,,"
(5) " "gr\'\'\'true,\',\'ee\"n""
(6) " ":::testme""
(7) " true"
All the values are strings as can be seen by their surrounding double quotes. They will not all end up that way though. A true or false will be converted to a boolean. The values surrounded by internal quotes will end up as strings. Then a value preceded with a : will end up as a symbol.
There are problems with the values at index 0 and 4. Index 0 should be this:
(0.0) "'d,og,f:asdf,:hello,",,\",,alsee'"
(0.1) nil
(0.2) nil
As you can see, the two commas at the end are gone. They have become the two nil values you see above. Then the string starts at the first single quote and ends at the last single quote, signifying that this value in the array is a string.
Then index 4 (" :good, false,,") should be this:
(4.0) " :good"
(4.1) " false"
(4.2) nil
(4.3) nil
The two commas at the end have become nil. Then " false" is it's own value which will later be converted to a boolean, while " :good" is also it's own value and will later be converted to a symbol.
To fix the problem with index 4 I have all the values run through a second regex. Here it is:
/^(\s*:(?:(?:[\w]+|\"[\s\S]+\"|'[\s\S]+')\s*)),([\s\S]*)$/
Instead of splitting this one I get the capture groups. It ends up returning this array for the value at index 4:
(4.0) " :good"
(4.1) " false,,"
That's what I wanted except for one problem. The value at index 4.1 (" false,,") has the two trailing commas which should be nil values in the array.
I need some help making a regex that will match any commas in a string that are side by side with unlimited white space around them and between them. The commas and their surrounding white space cannot be within matching single quotes or double quotes. I then need to capture the non-whitespace values from around those commas and count how many of those commas there are.
The values captured from around the commas will become their own values in the final array, while the commas that were counted will become nil values that are added to the final array.
"['d,og,f:asdf,:hello,"
,,\
",,alsee',,,'ho,la', "
-123,4,5.3
", true, :good, false,,, "
gr\
'\'
I count 4 strings. 3 in double quotes and the last one in single quotes?
You say this is broken down into smaller strings by your regx. But what about the characters outside the 4 strings?
Sorry, it looks a bit of a mess.
Try putting it all in a here document string and then breaking it down by a regx.
I finally figured it out myself. You can see how it fits in with the rest if you look at the description of the question above.
/^(([\s]*,)*)[\s]*((?::[\w]+)|(?::?(?:\"[\s\S]*\")|(?:'[\s\S]*')|false|true))?(([\s]*,)*)$/