On Thu, 16 Jul 2009 09:17:59 -0700 (PDT), Donato Azevedo <> wrote:
>Hi everyone,
>
>I've got a simple question to which Ive, to this point, not been able
>to solve:
>
>I have these regexes which I want to convert into a single one:
>
> if ( $raw_content =~ /Doc1(?:=rev)?
?<document1>.*?)\r\n
> Doc2(?:=rev)?
?<document2>.*?)\r\n
> Item
?<item>.*?)\r\n
> Data\s+doc1
?<data1>.*?)\r\n
> Data\s+doc2
?<data2>.*?)\r\n
> Obs
?<observation>.*?)\r\n
> Critic
?<criticality>.*?)\r\n
> Comments
?<comments>.*)
> /isx ||
> $raw_content =~ /Doc1(?:=rev)?
?<document1>.*?)\r\n
> Doc2(?:=rev)?
?<document2>.*?)\r\n
> Item
?<item>.*?)\r\n
> Data\s+doc1
?<data1>.*?)\r\n
> Data\s+doc2
?<data2>.*?)\r\n
> Obs
?<observation>.*?)\r\n
> Critic
?<criticality>.*)
> /isx ) {
>
>this is to match text that can either end in:
>
>Critic:foobartext
>
>or
>
>Critic:foo
>Comments:bar
>
>The problem seems to be the greediness of the last captures: I tried
>doing
>
>Critic
?<criticality>.*?)(\r\nComments
?<commen ts>.*))?
>
>and
>
>Critic
?<criticality>.*)(\r\nComments
?<comment s>.*))?
>
>but I must be missing something... It must be something quite simple
>I'd say.
>
>Well, any ideas?
Wow, looks complicated, but isin't. Yes, as DeRykus says,
you need a quantifier '?' (0 or 1) around a non capture grouping
of --> Critic

?<criticality>.*) in the first regex.
This will at least assign $+{criticality} a '' if there is no 'Critic:'
data (.*)and will assign (just like the $n vars I think) undef if there is no 'Critic:'
I haven't checked 5.10 much but, there may not even exist $+{criticality} if '?'
for the group is 0. Regex satisfied, but who knows how %+ hash is reset.
Probably exists, but set to undef, like its unamed capture counterpart.
Btw, whats this bizz: /(.*?)\r\n/s ??
-sln