On May 11, Gross, Stephan said:
>I wasn't clear last time. I wrote:
>>I want to match the following:
>>1) the letters "PT"
>>2) a space or nothing
>>3) a word that may or may not be in parentheses or even not exist
>>and return item #3 (which may be null)
>>Example:
>>PT (XYZ) or PT XYZ or PTXYZ or PT
>>should return "XYZ" except the last case, which should return "".
>
>>I can do everything except the parentheses case. Any ideas?
>
>The problem is the final parenthesis. If I use (.*) or (\w*) then it
>swallows the final parenthesis. I want to discard it if it is present.
>Also, the XYZ term may include spaces.
You need to express the "rules" more specifically or carefully next time.
($match) = $string =~ m{PT\s*\(?([^)]*)\)?};
For an explanation, I resort again to my 'explain' program.
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
PT 'PT'
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\(? '(' (optional (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^)]* any character except: ')' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\)? ')' (optional (matching the most amount
possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734