Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the string within brackets in Python

I have a sample string <alpha.Customer[cus_Y4o9qMEZAugtnW] active_card=<alpha.AlphaObject[card] ...>, created=1324336085, description='Customer for My Test App', livemode=False>

I only want the value cus_Y4o9qMEZAugtnW and NOT card (which is inside another [])

How could I do it in easiest possible way in Python? Maybe by using RegEx (which I am not good at)?

like image 326
user993563 Avatar asked Dec 20 '11 00:12

user993563


People also ask

How do you get a string between brackets in Python?

Method 1: Slicing and str. The simplest way to extract the string between two parentheses is to use slicing and string. find() . First, find the indices of the first occurrences of the opening and closing parentheses. Second, use them as slice indices to get the substring between those indices like so: s[s.

How do you get brackets in Python?

One approach to check balanced parentheses is to use stack. Each time, when an open parentheses is encountered push it in the stack, and when closed parenthesis is encountered, match it with the top of stack and pop it. If stack is empty at the end, return Balanced otherwise, Unbalanced.

Which bracket is used for string in Python?

The indexing operator (Python uses square brackets to enclose the index) selects a single character from a string.


1 Answers

How about:

import re  s = "alpha.Customer[cus_Y4o9qMEZAugtnW] ..." m = re.search(r"\[([A-Za-z0-9_]+)\]", s) print m.group(1) 

For me this prints:

cus_Y4o9qMEZAugtnW 

Note that the call to re.search(...) finds the first match to the regular expression, so it doesn't find the [card] unless you repeat the search a second time.

Edit: The regular expression here is a python raw string literal, which basically means the backslashes are not treated as special characters and are passed through to the re.search() method unchanged. The parts of the regular expression are:

  1. \[ matches a literal [ character
  2. ( begins a new group
  3. [A-Za-z0-9_] is a character set matching any letter (capital or lower case), digit or underscore
  4. + matches the preceding element (the character set) one or more times.
  5. ) ends the group
  6. \] matches a literal ] character

Edit: As D K has pointed out, the regular expression could be simplified to:

m = re.search(r"\[(\w+)\]", s) 

since the \w is a special sequence which means the same thing as [a-zA-Z0-9_] depending on the re.LOCALE and re.UNICODE settings.

like image 148
srgerg Avatar answered Sep 20 '22 13:09

srgerg