Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very interesting issue with regular expression Please Help Me :)
#1
Not Solved
Hello,

I have big (for me) problem with QTP and VBS. I've tried to find problem solution but Google didn't help.

My problem is:
From string: "37596 AndrzejCzwarty ldskajf@porirker\.plUnlocked2012-11-30 37568 AndrzejDrugi ksjhgjhsdgfui@woeijf\.plLocked2012-11-30 37554 AndrzejPierwszy kdjsfhsjd@sdklj\.plLocked2012-11-30 37582 AndrzejTrzeci lsdhflkadjhf@lsdkf\.plUnlocked2012-11-30 "

I want to take first value like "37554" which is after the word "Locked". Date in string is not always exist (or if exist, is changing), so it means that length is changing. Those numbers are random in every iteration.

So, in above string my variable ought to equals 37554.

Thank You for every help!!

dzordz
Reply
#2
Not Solved
Hi,

try this code:
Code:
Dim strStr

strStr = "37596 AndrzejCzwarty ldskajf@porirker\.plUnlocked2012-11-30 37568 AndrzejDrugi ksjhgjhsdgfui@woeijf\.plLocked2012-11-30 37554 AndrzejPierwszy" &_
              " kdjsfhsjd@sdklj\.plLocked2012-11-30 37582 AndrzejTrzeci lsdhflkadjhf@lsdkf\.plUnlocked2012-11-30 " & _
              "ZdenekSmetana ldskajf@porirker\.p Locked 88888 " & _
              "Jan Kowalski ldskajf@porirker\.p Locked " & _
               "Jan Nowak ldskajf@porirker\.p Locked 2012-11-30 "

Dim oRegx
Dim oMatches
Set oRegx = new  RegExp  

oRegx.pattern = "Locked(\d{4}-\d{2}-\d{2})*\s\d+\s"
oRegx.Global = true

Set oMatches = oRegx.Execute(strStr)

   For Each Match in oMatches   ' Iterate Matches collection.
      strValue =   Match.Value
      strValue1 = Mid( strValue, inStr( strValue, " " ) + 1 )
      MsgBox strValue & " =====> " & strValue1
   Next
This code assumes that the number that has to be extracted from data is always surrounded by two spaces, that is:
-exactly one space before the number, and exactly one space after the number.
Reply
#3
Not Solved
If ur string with date:
Code:
a = "37596 AndrzejCzwarty ldskajf@porirker\.plUnlocked2012-11-30 37568 AndrzejDrugi ksjhgjhsdgfui@woeijf\.plLocked2012-11-30 37554 AndrzejPierwszy kdjsfhsjd@sdklj\.plLocked2012-11-30 37582 AndrzejTrzeci lsdhflkadjhf@lsdkf\.plUnlocked2012-11-30 "

b = trim(mid( a , instr( instr(1,a , "Locked") , a , " ")   ))
c = mid( b , 1 , instr(1 , b , " ") )
msgbox c


Output i got : 37554

If ur string without date:

Code:
a = "37596 AndrzejCzwarty ldskajf@porirker\.plUnlocked2012-11-30 37568 AndrzejDrugi ksjhgjhsdgfui@woeijf\.plLocked 37554 AndrzejPierwszy kdjsfhsjd@sdklj\.plLocked2012-11-30 37582 AndrzejTrzeci lsdhflkadjhf@lsdkf\.plUnlocked2012-11-30 "

b = trim(mid( a , instr( instr(1,a , "Locked") , a , " ")   ))
c = mid( b , 1 , instr(1 , b , " ") )
msgbox c

Output i got : 37554


It worked for me , try ur luck Smile Smile

Thanks,
Harish Shenoy
Reply
#4
Not Solved
Hi,

I have encountered an issue, where in I am working on string.So need help.

String is str1 = "abcrrera 0200******011" , I am trying to use regular exp as the numbers in the string may change.

what is the Patrn that we need to pass?As i have written the code, however i think i am unable to handle "*" spl character in the above string.


Here is the function that i have written. However i am unable to get the pattern? Need help
Code:
Function RegExpTest(patrn ,string)

Dim regEx, Match, Matches  

       ''Create variable.

   Set regEx = New RegExp   ' Create regular expression.

   n = 0

   regEx.Pattern =patrn  ' Set pattern.

   regEx.IgnoreCase = True   ' Set case insensitivity.

   regEx.Global = True   ' Set global applicability.

   Set Matches = regEx.Execute(string)   ' Execute search.

   For Each Match in Matches   ' Iterate Matches collection.

      RetStr = RetStr & "Match found at position "

                  MSGBOX  RetStr

      RetStr = RetStr & Match.FirstIndex & ". Match Value is '"

                                  MSGBOX   RetStr

                                  RetStr = RetStr & Match.Value & "'." & vbCRLF

                                  MSGBOX   RetStr

          If Match.Value  = patrn  Then

                                                  msgbox patrn

                                                  msgbox  Match.Value

                                  n = n+1

                                  MSGBOX n

                                  End If

   Next

   RegExpTest = RetStr

    MSGBOX   RegExpTest

                If   n >1Then

                                                                Reporter.ReportEvent micPass, "Card Accepted text is Added to ER Area ", "Match Text: is" &patrn & " Repeats " & n & " Times"

                  msgbox patrn & " repeats " & n & "times"

      Elseif n=1 Then

                  msgbox patrn & " repeats " & n & "times"

            Reporter.ReportEvent micPass, "test is present", "Match Text: is" &patrn & " Repeats " & n & " Times"

                                                                  msgbox patrn & " repeats " & n & "times"

                Else

                Reporter.ReportEvent micFail, "test is  not present", "unMatch Text: " & patrn & " Repeats " & n & " Times"              

                msgbox patrn & " repeats " & n & "times"                          

                End If

End Function



This function is not working. Request you to kindly give a solution to this using reg expression only.
Thanks in advance.

I tried creating a new post, however i am not sure, i dont have permissions , so i am using the relevant tread.

Regards,
Ras
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need Any digit in xpath path using Regular expression Priyam 1 3,482 10-05-2016, 11:05 AM
Last Post: Ankur
  Regular expression to read two words in lowercase, uppercase and with and without spa sarahq49 1 3,350 04-09-2015, 01:56 AM
Last Post: sarahq49
  Regular expression and script optimisation Padmavathy 1 3,815 03-30-2015, 11:46 AM
Last Post: supputuri
  Regular expression in descriptive programming testernc 1 16,563 12-08-2014, 06:38 PM
Last Post: anshika.agarwal
  need a regular expression. anu05446 0 3,113 11-26-2014, 03:00 PM
Last Post: anu05446

Forum Jump:


Users browsing this thread: 1 Guest(s)