What does `?` Mean in this Perl regex?

I have a Perl regex. But I'm not sure what "?" means in this context.

m # (?: \ w +) #

What does ?it mean here?

+5
source share
5 answers

In this case, it is ?actually used in connection with :. ?:Grouping , at the beginning of grouping means grouping, but not fixing the text / template in parentheses (as in, it will not be stored in any backlinks, such as \1or $1, so you will not have access to the grouped text directly).

More specifically, it ?has three different meanings in a regular expression:

  • ? " " . , , - s?he, she, he, ? s ""

  • (+, *, ? {n,m}) ?, (.. , , )

  • A ? , . , : , . , ( ) :

    . : (?:text)
     B. Lookaround: (?=a) , ?! ?<= ?<! lookbehinds ( , ).
     C. : (?(condition)then|else).
     D. : a(?>bc|b)c ( abcc, abc, . )
     E. / regex: ?i, , ?-i . / , , ?im (i , m - ).
     F. : (?P<name>pattern), (?P=name). .NET regex (?<name>pattern).
     G. Comments: (?#Comment text). , , , ... ((?x)).

, ? - . (, \(?, .

+25

$perldoc perlreref:

(?:...) ()

YAPE::Regex::Explain:

C:\\Temp> perl -MYAPE::Regex::Explain -e \ 
"print YAPE::Regex::Explain->new(qr#(?:\w+)#)->explain"

The regular expression:

(?-imsx:(?:\w+))

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:
----------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
+7
+2

, (? . , (?, , , . Perl, Perl. perlre Perl.

+2
source

See the regular expression tutorial that is installed with each version of Perl (specifically in this section ).

+1
source

All Articles