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
Related
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
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())
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
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 "".
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.