Powershell regex replace only first hit -
i'm trying use regular expression replace first character after single hit, while using powershell.
no matter how try, can't seem make work. here's i'm talking about:
code:
$info = 'ab/f/*zxcvbn/mtf/ ---' $regex = [regex]'/*' $regex.replace($info,"/c",1) $regex
output:
/cab/f/*zxcvbn/mtf/ ---
i'm trying replace /f
in expression /c
, fails every time.
i'm using /*
since don't know character find after first /
that's want replace in end of day.
i pretty sure pretty simple but, can see, i'm, not familiar enough regular expressions.
ok, rather comment guess i'll add answer. can use negative lookbehind make sure there no /'s before matching, match first one. also, noah stated * not wildcard, . is. match / plus 1 character not have / anywhere before in string:
"(?<!/.*)/."
so in context code, this:
$info = 'ab/f/*zxcvbn/mtf/ ---' $regex = [regex]"(?<!/)/." $regex.replace($info,"/c",1)
those lines output:
ab/c/*zxcvbn/mtf/ ---
edit: regex broken down @ regex101: http://regex101.com/r/ti7on1/1
Comments
Post a Comment