Regexp object:-
Regexp object is used to integrate QTP with regular expression utilization. This object consist of 3 properties and 3 methods
A) Pattern:- we can use this property to store expected expression for matching.
Syntax:- set r=new regexp
r.pattern=”regexp”
r.global=true/false
B) Global:-
We can use this property to specify pattern matching on given text until end of text.
Syntax:- set r=new regexp
r.pattern=”regexp”
r.global=true/false
c) Ignorecase:-
We can use this property to specify case sensitiveness.
Syntax:- set r=new regexp
r.pattern=”[A-Z][a-z]+”
r.global=true
r.ignorecase=true
In example code, a string will be matched with pattern which start’s with lowercase also. Because ignorecase is true.
D) Execute( ):-
We can use this method to apply given pattern on given text and find matched values in text.
Ex:- set r=new regexp
r.pattern=”[0-9]+”
r.global=true
x=”jky345 acd 23 jkil34dd”
set y=r.execute(x)
for each z in y
print(z.value)
next
E)Test:-
We can use this to apply given pattern on given text for matching. If pattern was matched with matched with text , then this method returns true.
Ex:- set r=new regexp
r.pattern=”[0-9]+”
r.global=true
x=”544755”
if r.test(x) then
print(“matched”)
else
print(“mismatched”)
end if
A) Replace( ):-
We can use this method to replace matched text with other text.
Ex:- set r=new regexp
r.pattern=”[0-9]+”
r.global=true
x=”my name is 5”
r.replace(x,”A”)
print(x)
Ex:-8
To copy one file text into another file.
Option explicit
Dim fso,fo,l,fo1,fo2
Set fso=createobject(“scripting.filesystemobject”)
Set fo1=fso.opentextfile(“c:\sample1.txt”,1,false)
Set fo2=fso.opentextfile(“c:\sample2.txt”,2,true)
While fo1.atendofstream< >true
l=fo1.readline
fo2.writeline(l)
wend
fo1.close
fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing
Ex:-9
Write vbscript in qtp , to copy numbers in file1 into file2.
Option explicit
Dim fso,fo1,fo2,l,nums,num
Set fso=createobject(“scripting.filesystemobject”)
Set fo1=fso.opentextfile(“c:\sample1.txt”,1,false)
Set fo2=fso.opentextfile(“c:\sample2.txt”,2,true)
While fo1.atendofstream< >true
l=fo1.readline
set r=new regexp
r.pattern=”[0-9]+”
r.global=true
set nums=r.execute(l)
for each num in nums
fo2.writeline(num.value)
next
set numns=nothing
set r=nothing
wend
fo1.close
fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing
Ex:-10
To copy alphanumeric only from file1 to file2.
Option explicit
Dim fso,fo1,fo2,l,nums,num,r
Set fso=createobject(“scripting.filesystemobject”)
Set fo1=fso.opentextfile(“c:\sample1.txt”,1,false)
Set fo2=fso.opentextfile(“c:\sample2.txt”,2,true)
While fo1.atendofstream< >true
l=fo1.readline
set r=new regexp
r.pattern=”[0-9]+[a-z]+”
r.global=true
set nums=r.execute(l)
for each num in nums
fo2.writeline(num.value)
next
wend
fo1.close
fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing
Ex:-11
To copy dates in file1 into file2. Here date is “mm/dd/yy”
Option explicit
Dim fso,fo1,fo2,l,nums,num,r
Set fso=createobject(“scripting.filesystemobject”)
Set fo1=fso.opentextfile(“c:\sample1.txt”,1,false)
Set fo2=fso.opentextfile(“c;\sample2.txt”,2,true)
While fo1.atendofstream< >true
l=fo1.readline
set r=new regexp
r.pattern=”(([0][0-9])|([1][0-2]))[/](([0][0-9])|([1][0-9])|([2][0-
9])|([3][0-1]))[/][0-9]{2}”
r.global=true
set nums=r.execute(l)
for each num in nums
fo2.writeline(num.value)
next
set nums=nothing
set r=nothing
wend
set fo1=nothing
set fo2=nothing
set fso=nothing
Please tell the output of each program to understand it better..
ReplyDeleteif possible please could you send some more example with explanation with frame wordk
ReplyDelete