Forum Discussion

gongyan's avatar
gongyan
Copper Contributor
Dec 24, 2021

Regarding the problem of -match not updating the $Matches variable after matching

这是我代码的一部分

 

 

 

$Matches = 0
    $url_0_0_regex = '(?<=a href="\.\./\.\./\.\./).+?(?=">2<)'
    #$url_1_context
    #sleep -Seconds 50
    $result_bool = $url_1_context  -match $url_0_0_regex
    if($result_bool ){
        $Matches
        
        sleep -Seconds 50

 

 

 

After I set $Matches to 0, and after the -match operation succeeded, I output the value of $Matches, but it still showed 0 instead of the result of a successful match.

According to the document description of the comparison operator, the value of $Matches will be automatically updated after the -match operation is successful. I don't understand what happened in the middle that caused this situation.

I have tried [regex]::, but the result of the Match function of [regex]:: makes me desperate. [regex]:: does not seem to support the use of backward matching in regular expressions. Please help me, thanks a lot!

  • $Matches hold the success matches and its a hashtable
    maybe the 0 you are getting is the first key of the hashtable
    try $Matches.Values
    can you provide a string of matching?
  • farismalaeb's avatar
    farismalaeb
    Steel Contributor
    $Matches hold the success matches and its a hashtable
    maybe the 0 you are getting is the first key of the hashtable
    try $Matches.Values
    can you provide a string of matching?
    • gongyan's avatar
      gongyan
      Copper Contributor

      farismalaeb

       

      Hello, thank you for your reply!

      $url_1_context is the html source code of a web page. I use the regular expression $url_0_0_regex to match, and output the value of $Matches by judging the result of -match if($result_bool ), but obviously, the value of $Matches is another- The value after the match operation; it matches another regular expression, but does not match the regular expression represented by $url_0_0_regex.

      In addition, it should be noted that the code here is in a function (get_content), and this function (get_content) is called by another function (get_url_name), before calling this function (get_content), once- match operation.

       

       

    • gongyan's avatar
      gongyan
      Copper Contributor

      farismalaeb 

      It is possible to obtain the desired result correctly by matching alone; an example is as follows:

       

       

       

       

      $a = '<tr><td align="left"><div class="pages"><a href="javascript&colon;#" class="gray"><</a><a href="javascript&colon;#" class="gray">上一頁</a><a><U>1</U></a><a href="../../../read.php?tid=4651187&amp;page=2">2</a>'
      $a -match 'a href="\.\./\.\./\.\./.+?(?=">2<)'
      $Matches
      
      out
      True
      
      Name                           Value                                                                                                                                                                                                                                                                                   
      ----                           -----                                                                                                                                                                                                                                                                                   
      0                              a href="../../../read.php?tid=4651187&amp;page=2

       

       

       

       So, I guess this may be more related to the scope of the variable, but I cannot change the scope of $Matches. Can the -match operation save the result to other custom variables instead of updating it to the $Matches variable? 

      • farismalaeb's avatar
        farismalaeb
        Steel Contributor

        gongyan 

        I hope I understand your explination, but here are my finding.

        The regex example you provide are different and surely they wont match. 

        Which one are you using '(?<=a href="\.\./\.\./\.\./).+?(?=">2<)' or'a href="\.\./\.\./\.\./.+?(?=">2<)'

        I checked the result in regex101.com. The a href="\.\.\/\.\.\/\.\.\/.+?(?=">2<) match the following string a href="../../../read.php?tid=4651187&amp;page=2

        and the (?<=a href="\.\.\/\.\.\/\.\.\/).+?(?=">2<) match withread.php?tid=4651187&amp;page=2

        So  which regex are you using and showing the wrong result.

        I think you need to review your regex query, it doesn't matter if you are passing the result from one function to another.

         

Resources