prosseek
prosseek

Reputation: 190769

Making some detail explanations in python string

With Java, I can split the string and give some detailed explanations

String x = "a" + // First
           "b" + // Second
       "c";  // Third

// x = "abc"

How can I make the equivalence in python?

I could split the string, but I can't make a comment on this like I do with Java.

x = "a" \
"b" \
"c"

I need this feature for explaining regular expression usage.

Pattern p = Pattern.compile("rename_method\\(" + // ignore 'rename_method('
                        "\"([^\"]*)\"," +    // find '"....",' 

Upvotes: 2

Views: 100

Answers (2)

mgilson
mgilson

Reputation: 309899

This

x = ( "a" #foo
      "b" #bar
    )

will work.

The magic is done here by the parenthesis -- python automatically continues lines inside of any unterminated brakets (([{). Note that python also automatically concatenates strings when they're placed next to each other (We don't even need the + operator!)-- really cool.

Upvotes: 8

mipadi
mipadi

Reputation: 410652

If you want to do it specifically for regular expressions, you can do it pretty easily with the re.VERBOSE flag. From the Python docs (scroll down a bit to see the documentation for the VERBOSE flag):

charref = re.compile(r"""
 &[#]                # Start of a numeric entity reference
 (
     0[0-7]+         # Octal form
   | [0-9]+          # Decimal form
   | x[0-9a-fA-F]+   # Hexadecimal form
 )
 ;                   # Trailing semicolon
""", re.VERBOSE)

Upvotes: 4

Related Questions