Using RegExp to Search Arrays

Results 1 to 2 of 2

Thread: Using RegExp to Search Arrays

  1. #1
    Join Date
    Dec 1969

    Default Using RegExp to Search Arrays

    Hi can anyone help. I have a script which I need to search all records in a recordset and return one answer if records are found and another answer if they are not. I want to use the RegExp VBScript runtime object and have prepared the following script:<BR><BR>&#039;create a loop for the number of records in the recordset put the value in strFaculty field into strChkFaculty array for each record and append to the variable strSearch so strSearch will contain all text to be searched<BR><BR>For i=0 to rs.RecordCount<BR> strChkFaculty = strFaculty<BR> strSearch = strSearch & strChkFaculty(i)<BR>Next<BR><BR>&#039;create new RegExp object set the pattern property to search for "Bu" and Global to "true" to search for all occurances. Perform search on strSearch.<BR><BR>set objRegExp = New RegExp<BR> objRegExp.Pattern = "Ar"<BR> objRegExp.Global = True<BR> <BR> blnFind = objRegExp.Test(strSearch)<BR><BR>&#039;if "Ar" is found print one message else print another<BR><BR>if blnFind Then<BR> Response.Write("test found")<BR> Else <BR> Response.Write("test not found")<BR> End if<BR><BR>However, this always results in test not found even when records contain "Ar" in strFaculty.<BR>Does anyone have any suggestions. <BR>Many thanks

  2. #2
    Join Date
    Dec 1969

    Default RE: Using RegExp to Search Arrays

    First off, I&#039;d just throw the recordset into an array using getrows and then loop through that with my regex object. Something like this..<BR><BR>Set rs = conn.execute("Select Faculty From Tablename")<BR>if not rs.eof then<BR> aFaculty = rs.getrows<BR>end if<BR>rs.close: set rs = nothing<BR><BR>if not isEmpty(aFaculty) then<BR> set oRegExp = new RegExp<BR> = true<BR> oRegExp.ignorecase = true<BR> oRegExp.Pattern = "AB"<BR><BR> iMax = ubound(aFaculty,2)<BR> for i = 0 to iMax<BR> if oRegExp.test(aFaculty(0,i)) then<BR> &#039;found a match<BR> iMatch = i<BR> exit for<BR> end if<BR> next<BR><BR> if lenb(trim(iMatch & "")) &#062; 0 then<BR> response.write "Match found at " & aFaculty(0,iMatch)<BR> end if<BR><BR> set oRegExp = nothing<BR><BR>end if<BR><BR>Now, I don&#039;t know for sure that this would work because it just came off the top of my head, but I think the logic is pretty straightforward. I imagine that you could test each element in the array and then for each match, append it to a match string like 1&#124 3&#124 7&#124 12 are all matches. Then use split to create an array and use those indexes to reference your aFaculty to display any matches.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts