Monica
Monica

Reputation: 1599

What are extended regular expressions?

I am studying for an exam and one of the topics is on RegEx. There is a modifier, x, with the description 'uses extended regular expressions,' but it doesn't explain what this means. I can't really find anything on Google that fits the context of my study material.

On a quiz, one of the questions asked what modifier allows the use of comments in RegEx, and the correct(?) answer was x.

Could someone please help me make sense of this? Thank you.

Edit: I meant x in the context of [gix], where x is described as 'uses extended regular expressions.'

Upvotes: 8

Views: 4797

Answers (4)

Dave Cross
Dave Cross

Reputation: 69304

You seem to be confusing two different concepts here.

The /x option to the match (m//) and substitution (s///) operators in Perl tells the regex parse to ignore white space. This is used to make your regex more readable.

Extended patterns are additions to the Perl regex engine so that it supports things (like look-ahead and look-behind) that aren't usually supported in regex tools.

Upvotes: 1

parthi
parthi

Reputation: 156

/x to ignore whitespace that is neither backslashed nor within a character class. to make your regex more readable

Upvotes: 2

hwnd
hwnd

Reputation: 70732

As stated in the perlre documentation:

/x tells the regular expression parser to ignore most whitespace that is neither backslashed nor within a character class. You can use this to break up your regular expression into (slightly) more readable parts...

The modifier improves readability, and allows us to add explanatory comments.

 /^            # the beginning of the string
   (?:         # group, but do not capture:
       foo     #   match 'foo'
      |        #  OR
       bar     #   match 'bar'
   )           # end of grouping
  $            # the end of the string
 /x;

Even without the /x modifier you can enclose comments.

/foo(?# match foo)/

Upvotes: 4

Miller
Miller

Reputation: 35208

As documented in perlretut (Just search for /x):

Long regexps like this may impress your friends, but can be hard to decipher. In complex situations like this, the //x modifier for a match is invaluable. It allows one to put nearly arbitrary whitespace and comments into a regexp without affecting their meaning. Using it, we can rewrite our 'extended' regexp in the more pleasing form

    /^
          [+-]?         # first, match an optional sign
          (             # then match integers or f.p. mantissas:
               \d+\.\d+  # mantissa of the form a.b
              |\d+\.     # mantissa of the form a.
              |\.\d+     # mantissa of the form .b
              |\d+       # integer of the form a
          )
          ([eE][+-]?\d+)?  # finally, optionally match an exponent
      $/x;

Upvotes: 8

Related Questions