Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I break my Perl regex into multiple lines in my code?

Tags:

regex

perl

I was just wondering if I am able to break up a long regular expression that I have in my Perl code so that it is written over several lines? I just want the readability and compactness to be intact for anyone that might view my code after its completion. I am looking for something analagous to the way in which strings are broken up over several lines in Perl. For example:

print "This is a string that is ". #1st line
      "very long, so it is on 2 lines!"; #2nd line
# prints = "This is a string that is very long, so it is on 2 lines!" 

I am not sure how to do this with a regex since it does not use quotes. If I press enter I am guessing it will put a new line character in my regex making it erroneous. I would like to do something along the lines of:

if($variable_1 = /abcde_abcde_abdcae_adafdf_      #1st regex line
                 abscd_casdf_asdfd_....asdfaf/){ #regex continued
    # do something
} # regex looking for pattern = abcde_abcde_abdcae_adafdf_abscd_casdf_asdfd_....asdfaf
like image 763
thomas.cloud Avatar asked Jul 25 '12 14:07

thomas.cloud


People also ask

Is Perl good for regex?

Perl has regex built directly into the language. Other programming languages need a module/library to use them. raw Perl without any modules, can handle regex.

What is \d in Perl regex?

The Special Character Classes in Perl are as follows: Digit \d[0-9]: The \d is used to match any digit character and its equivalent to [0-9]. In the regex /\d/ will match a single digit. The \d is standardized to “digit”.

How do I match a new line in a regular expression in Perl?

Use /m , /s , or both as pattern modifiers. /s lets . match newline (normally it doesn't). If the string had more than one line in it, then /foo. *bar/s could match a "foo" on one line and a "bar" on a following line.

What regex engine does Perl use?

As of Perl 5.10, PCRE is also available as a replacement for Perl's default regular-expression engine through the re::engine::PCRE module.


2 Answers

Use the Whitespace-Insensitive Modifier

The perlre(1) manual page says:

The "/x" modifier itself needs a little more explanation. It 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.

You can use this to create multiline expressions. For example:

/
  abcde_abcde_abdcae_adafdf_    # Have some comments, too. 
  abscd_casdf_asdfd_....asdfaf  # Here's your second line.
/x

Literal Spaces

If your match will contain spaces, you need to make them explicit in your regular expression when using the /x modifier. For example, /foo bar baz quux/x won't match a space-separated string the way you might expect. Instead, you need something like the following:

print "true\n" if
    /
        foo
        \s+  # You need explicit spaces...
        bar
        \s+  # because the modifier will...
        baz
        \s+  # otherwise ignore literal spaces.
        quux
    /x;
like image 128
Todd A. Jacobs Avatar answered Sep 24 '22 01:09

Todd A. Jacobs


Yep, see the /x modifier in the perlre man page.

like image 24
Alex Howansky Avatar answered Sep 24 '22 01:09

Alex Howansky