How to strip out particular text using VBScript - vbscript

I have got below text as string
test = "<span class='convert'>USD 30</span>"
I need to write a function in VBSCript which will take above string as input and will return USD 30 as output.
Please suggest

output = Replace (Replace(test , "<span class='convert'>",""),"</span>","")

If your input text is more complicated than the example you've given, I would recommend using an XML library such as MSXML to parse the text. Otherwise, you can use a regex
Const test = "<span class='convert'>USD 30</span>"
dim regex: set regex = new RegExp
regex.pattern = ">([^<]*)"
dim matches: set matches = regex.execute(test)
dim output: output = Empty
if matches.Count <> 0 then
output = matches(0).submatches(0)
end if
Response.Write output

Related

Validating a string vbscript

I am trying to search a string to make sure it does not contain a numbers followed by a comma followed by numbers and containing a dot and 2 decimal places, e.g. 32,000.00. At the moment, I have it searching to see if the string contains a dot only. How do I go about searching the criteria above?
Root_RefundAmount = Root_TaxDataSummary.SlvObject("RefundAmount").GetROProperty("text")
refund = InStr(1,Root_RefundAmount,".")
If refund > 0 Then
pass
Else
fail
End If
I believe a simple Regular Expression Test will provide you what you want:
Dim objRegEx, strValue
'Create Regular Expression object to parse the file
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.MultiLine = True
objRegEx.Pattern = "\d+\,\d+\.\d{2}"
strValue = "e.g. 32,000.00."
MsgBox objRegEx.test(strValue) '<- True
strValue = "e.g. 3200000."
MsgBox objRegEx.test(strValue) '<- False
The above will result in a Boolean response (True/False).
Adapting it to your routine...
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.MultiLine = True
objRegEx.Pattern = "\d+\,\d+\.\d{2}"
Root_RefundAmount = Root_TaxDataSummary.SlvObject("RefundAmount").GetROProperty("text")
If Not objRegEx.test(Root_RefundAmount) Then
'Pattern NOT Found
Else
'Pattern IS Found
End If
To explain the Pattern:
"\d+\,\d+\.\d{2}"
\d = Any Number 0 through 9...
"A numbers followed by a comma followed by numbers and containing a dot and 2 decimal places"{Assume you mean 2 Numbers}... Depending on your input the above is much easier to look at but it will also match numbers such as 12345,12345.99 which may or may not be desirable.
If you need to be precise then you may want to consider the below Regex:
"\d{1,3}(?:\,\d{3})*\.\d{2}"
This will match numbers from 0.00 to 999,999,999.99 etc in typical numeric format...
If you want to specifically look for numbers above 1,000 (With a Comma) Then change the Star '*' to a Plus '+' to mean One or More are Required...
"\d{1,3}(?:\,\d{3})+\.\d{2}"
Or for an even more specific Regex:
(?:[1-9]|[1-9][0-9]|[1-9][0-9]{2})(?:\,[0-9]{3})+\.[0-9]{2}
'OR
(?:[1-9]|[1-9]\d|[1-9]\d{2})(?:\,\d{3})+\.\d{2}
For a little more info on Regular Expressions and the '\d' Character Class, Please take a look at the below links:
http://www.regular-expressions.info/shorthand.html
https://msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx#Anchor_9

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

Is there a direct funtion to count numbers inside a string?

I have a string that contains digits inside. For example, "adf20j83n,m3jh2k9". Is there a direct way to count the number of digits inside the string. As in my example, it should give me "7" as an output.
Also, I have tried RegExp but it's not working in VBScript in QTP.
Btw, I'm not looking for loops and stuff like that. Just a direct way, or a suggestion to make this RegExp work in QTP.
You'll probably need to create the COM object via its ProgId:
Dim re
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\d"
re.Global = True
MsgBox "Digits: " & re.Execute("adf20j83n,m3jh2k9").Count
Output:
Digits: 7
I know it's techically what you said but it's a good way nonetheless
Try using this function:
Public Sub CountNumeric(ByVal input As String)
Dim numericCount As Integer = 0
For Each c As Char In input
If Char.IsDigit(c) Then numericCount += 1
Next
MessageBox.Show(String.Format("Number of numerics : {0}", numericCount)
End Sub
EDIT:
You could also try this:
Dim charColl As MatchCollection = Regex.Matches(input , "^\d+$")
Console.WriteLine(charColl.Count.ToString())

Replace characters suing VB script

I want to replace a particular value followed by TRN* to some other value.How to do the coding for the same?..Please provide an example.
For example :
TRN*12345*34444~
This is a segment in my file(like this I have many TRN* segments in my file).I want to replace the segment after TRN* and before next * (ie.12345)with some other value.
Is there any way to do this by using vbscript?
Thanks in advance
The regular expression way of safetyOtter is the way to go, you only have to fiddle with the pattern and replacement string:
originalString = "TRN*12345*34444~"
replaceValue = "78910"
Set re = new RegExp
re.Pattern = "(.*TRN\*)([^*]+)(.*)"
re.IgnoreCase = False
' This keeps the first and last part between parenthesis, but replaces the middle part
newString = re.Replace(originalString, "$1" & replaceValue & "$3")
msgbox newString
' result: TRN*78910*34444~
Something like this should work, may have to tweak, can't test it at the moment
Set regEx = New RegExp
regEx.Pattern = "TRN\*.*?\*"
regEx.IgnoreCase = True
replStr = "TRN*" & SomeOtherValue& "*"
ReplaceTest = regEx.Replace(YourStringHere, replStr)
Edit:
if you are looking to replace a specific number with another, a simple:
YourStringHere = Replace(YourStringHere,"TRN*12345*","TRN*67890*")
Is enough

Is there such thing as Server.FileNameEncode?

Is there something like this, that only leaves numbers, letters and underscore for spaces?
Server.URLEncode isn't quite what i'm looking for.
There is no an integrated function as you described in ASP.
But you can do with regular expressions.
May be as follows:
Function FileNameEncode(ByVal strFileName)
Dim oReg
Set oReg = New RegExp
oReg.IgnoreCase = True
oReg.Global = True
oReg.Pattern = "[^a-z\d\s.]+"
FileNameEncode = Replace(oreg.Replace(strFileName, ""), " ", "_")
Set oReg = Nothing
End Function
'FileNameEncode("letters é$- 123ÖÇ.bat") returns "letters__123.bat"
If you pass filename without extension as parameter, you could remove the dot char from pattern.

Resources