Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Why Powershell Regex "\$\$\$\$" cannot match string "$$$$"

$
0
0

Today I try to write powershell script to replace some string like: $abcdef$ to $(abcdef) .

But when I try to using "\$[0-9a-zA-Z]+\$" to match the target, powershell cannot give the correct result.

Tests I did for this as bellow:

CLS;
"$" -match "\$";
"0$" -match "\d\$";
"ab$" -match "\w+\$";
"$$" -match "\$\$";
"$0$" -match "\$\d\$";
"$ab$" -match "\$\w+\$";

The result I got is:

True

True

True

False

False

False

The result shows if '$' is the first but not the only char of String, then "\$" cannot match it. I also test it in C# console:

Regex test1 = new Regex(@"\$");Console.WriteLine(test1.IsMatch("$"));Regex test2 = new Regex(@"\d\$");Console.WriteLine(test2.IsMatch("0$"));Regex test3 = new Regex(@"\w+\$");Console.WriteLine(test3.IsMatch("ab$"));Regex test4 = new Regex(@"\$\$");Console.WriteLine(test4.IsMatch("$$"));Regex test5 = new Regex(@"\$\d\$");Console.WriteLine(test5.IsMatch("$0$"));Regex test6 = new Regex(@"\$\w+\$");Console.WriteLine(test6.IsMatch("$ab$"));

And all six tests return True.

Anyone can help me on this? I want know is this a Powershell Bug or I need using special format to create this Regex.

Thanks.


Softnado



Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>