perl.beginners
[Top] [All Lists]

Re: Regular Expression - exclude character from character class ?

Subject: Re: Regular Expression - exclude character from character class ?
From: Chas. Owens
Date: Fri, 9 Jan 2009 11:53:27 -0500
Newsgroups: perl.beginners

On Thu, Jan 8, 2009 at 23:25, howa <howachen@xxxxxxxxx> wrote:
> Hello,
>
>
> Consider the string:
>
> $s = '[[2003]] abc [[2008]] "def"';
>
>
> I want to extract 2008 and def, so using
>
>
> \[\[([\w\W^\]]+?)\]\]\s"(.+?)"
>
> The regex match all string, even thought I have added to exclude: ^\]
> inside the character class.


^ only makes a character class negative if comes at the beginning of
the class.  Also, having both \w and \W is meaning less.  You are
saying to match characters in the \w class, characters not in the \w
class, ^, and ] (and both ^ and ] are both part of \W).  I believe you
want:

$s =~ /\[\[([^\]]+)]] "([^"]+)"/;

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

<Prev in Thread] Current Thread [Next in Thread>