Forum Discussion
Regarding the problem of -match not updating the $Matches variable after matching
- Dec 24, 2021$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?
maybe the 0 you are getting is the first key of the hashtable
try $Matches.Values
can you provide a string of matching?
- gongyanDec 25, 2021Copper Contributor
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:#" class="gray"><</a><a href="javascript:#" class="gray">上一頁</a><a><U>1</U></a><a href="../../../read.php?tid=4651187&page=2">2</a>' $a -match 'a href="\.\./\.\./\.\./.+?(?=">2<)' $Matches out True Name Value ---- ----- 0 a href="../../../read.php?tid=4651187&page=2So, 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?
- farismalaebDec 27, 2021Iron Contributor
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&page=2
and the (?<=a href="\.\.\/\.\.\/\.\.\/).+?(?=">2<) match withread.php?tid=4651187&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.
- gongyanJan 07, 2022Copper Contributor
Thank you for your answer.
I discovered later that this is caused by the differences in the implementation of regularization in different programming languages. I use notepad++ software to verify whether the regular expressions can match, but some regular expressions cannot match the expected results correctly in PowerShell.
And in the case of PowerShell, using the -match operator to match and the [regex::] class to match, in some cases, it is not possible to get consistent results.
- gongyanDec 25, 2021Copper Contributor
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.