On Jul 13, 7:56 pm, dreeschkind <dreeschk...[ at ]discussions.microsoft.com> wrote:
[Quoted Text] > Thanks for sharing this. > > I'd like to add one thing: In your last example you can also use a string in > single quotes instead of double quotes with backticks to avoid variable > interpolation: > > PS> $str = "<i>big</i>" > PS> $str = [regex]::Replace($str, "<i>(\w+)</i>", '$1 $1'); > PS> $str > big big > > -- > greetings > dreeschkind > > "John Cook" wrote: > > I've run into several gotchas using regular expressions in PowerShell. After > > getting past some roadblocks (thanks in part to help given here) I wrote up > > some notes in case other people have similar questions, especially those > > coming from Perl. > > > http://odin.mdacc.tmc.edu/~cook/regex.html Or just this:
PS C:\> $str = "<i>big</i>" PS C:\> $str -replace "<i>(\w+)</i>",'$1 $1' big big
It's a small thing, but I tend to prefer this syntax:
$reg = [regex]'\woo\w' $reg.matches($str)
I find it a bit easier to follow scripts with lots of long regexes using that syntax, since the regex name appears at the start rather than the end of the statement, and so can be scanned a bit more easily. Just a personal preference, though.
-Hecks
|