python - Extracting elements inside and between brackets -


I have the following string and what would I like to extract between elements (xx = "yy") and parentheses. Here is an example: [caption id = "get it", align = "and" width = "and" caption = "and this"] also [/ caption]

I've tried the following code but I'm quite a noob with regex.

  re.sub (r '\ [caption id = "(. *)" Align = "(. *)" Width = "(. *)" Caption = "(. *) "\] (. *) \ [\ / Caption \] ',' token:% 1% 2% 3% 4% 5 ', self-sufficiency, again. NICACAS)   

In advance Thanks a lot!

This is probably not working for you because . * Greedy [^ "] * try instead in its place. [^"] Excerpt is a set of all characters except characters, as well You have stated in the comments, the token syntax is \\ n , not % n . Try it:

  re.sub (r '\ [caption id = "([^" * *] "align =" ([^ "] *)" width = "([^ "] *" Caption = "([^"] *) "\] (. *) \ [\ Caption \] ',' token: \\ 1 \\ 2 \\ 3 \\ 4 \\ 5" , Self.content, re.IGNORECASE)   

Span the content of the caption tag multiple lines? If they do this, then . * on new lines Instead, you need us something like [^ \ x00] * . [^ \ x00] means the set of all charchators except for a red letter. / P>

  re.sub (r '\ [caption id = "([^"] *] ([^ \ x00] *) \ [\ / \ "\") caption \]', 'Tow Sans: \\ 1 \\ 2 \\ 3 \\ 4 \\ 5 ", self revolution, re. NICACAS)   

On the off chance that the actual characters in your strings are legally Can be included, you will need to use the re.DOTALL flag instead.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -