Group:  English: General ยป microsoft.public.windows.powershell
Thread: Regular expression notes

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Regular expression notes
John Cook 13.07.2007 16:18:01
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

RE: Regular expression notes
dreeschkind 13.07.2007 18:56:08
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:

[Quoted Text]
> 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
>
Re: Regular expression notes
hecks[ at ]hotmail.co.uk 13.07.2007 21:10:09
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

Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net