in reply to matching a line with ' and "
m/=(?:"[^"']+")|(?:'[^"']+')/;
As you see we check for both valid possibilities (hence the use of |). This means that we do not have to save what was the first quote.
Here is a full explanation (with thanks to YAPE::Regex::Explain):
The regular expression: (?-imsx:=(?:"[^"']+")|(?:'[^"']+')) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- = '=' ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- [^"']+ any character except: '"', ''' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- ' '\'' ---------------------------------------------------------------------- [^"']+ any character except: '"', ''' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ' '\'' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Please note this does not work with forms such as var1='"1"' or similar.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
---|