Saturday 29 August 2015

Reg Exp Examples: VBScript, QTP

Regular Expression: \d{10}
''''1. pat="\d{3}\-\d{4}"
pat="\d{10}"
str1="abc1234456lhlh1232159876-987644"
Set reg=new RegExp
reg.Global=True
reg.ignoreCase=True
reg.pattern=pat
Set mat=reg.Execute(str1)
For i=0 to mat.count-1
    msgbox mat(i).value
Next
===============
To check whether the text has any URL in "www.something.com" using regexp

str= "asdf asfa asdf www.Rajnikanth_Raskela.com test  testasdfasd asdf asdf"
'' if the text ''str'' contains any url format in it, the url will be returned in retURL variable.
Call CheckURL(str, retURL)
msgbox retURL
Function CheckURL(strFindURL, retURL)
    CheckURL= False
    Set rg=new RegExp
    rg.ignoreCase= True
    rg.global=false
    rg.pattern= "www\.\w+\.com"
    Set c=rg.execute(strFindURL)
    If c.Count=0 Then
        msgbox "No URL is found in the given string.."
        else
        For i=0 to c.count-1       
            retURL=  c(i).value
            CheckURL= True
            Exit For
        Next
    End If
End Function
============================
Replace multiple spaces with single space using RegExp..

str="Removing      multiple    (multiple     spaces here    )    spaces      with    single (single space here) space   character."&_
"       m done  !   !       &"
Call replaceMultiSpace(str)
Function replaceMultiSpace(strIO)
    replaceMultiSpace=""
    If strIO="" Then
        MsgBox "Invalid input string.."
        Exit Function
    End If
    Set rg=New RegExp
    rg.Global=True
    rg.IgnoreCase= True
    rg.Pattern= "\s{2,}"
     output=rg.Replace(strIO, " ")
     If output<>"" Then    
        replaceMultiSpace=output
         MsgBox "Refined Output: "& replaceMultiSpace
     End If   
     Set rg=Nothing
 End Function

No comments:

Post a Comment