Get the string within brackets in Python
Question:
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)?
Answers:
You could use str.split
to do this.
s = "<alpha.Customer[cus_Y4o9qMEZAugtnW] active_card=<alpha.AlphaObject[card]
...>, created=1324336085, description='Customer for My Test App',
livemode=False>"
val = s.split('[', 1)[1].split(']')[0]
Then we have:
>>> val
'cus_Y4o9qMEZAugtnW'
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:
[
matches a literal [
character
(
begins a new group
[A-Za-z0-9_]
is a character set matching any letter (capital or lower case), digit or underscore
+
matches the preceding element (the character set) one or more times.
)
ends the group
]
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.
This should do the job:
re.match(r"[^[]*[([^]]*)]", yourstring).groups()[0]
You can also use
re.findall(r"[([A-Za-z0-9_]+)]", string)
if there are many occurrences that you would like to find.
See also for more info:
How can I find all matches to a regular expression in Python?
your_string = "lnfgbdgfi343456dsfidf[my data] ljfbgns47647jfbgfjbgskj"
your_string[your_string.find("[")+1 : your_string.find("]")]
courtesy: Regular expression to return text between parenthesis
You can use
import re
s = re.search(r"[.*?]", string)
if s:
print(s.group(0))
How about this ? Example illusrated using a file:
f = open('abc.log','r')
content = f.readlines()
for line in content:
m = re.search(r"[(.*?)]", line)
print m.group(1)
Hope this helps:
Magic regex : [(.*?)]
Explanation:
[
: [
is a meta char and needs to be escaped if you want to match it literally.
(.*?)
: match everything in a non-greedy way and capture it.
]
: ]
is a meta char and needs to be escaped if you want to match it literally.
This snippet should work too, but it will return any text enclosed within "[]"
re.findall(r"[([a-zA-Z0-9 ._]*)]", your_text)
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)?
You could use str.split
to do this.
s = "<alpha.Customer[cus_Y4o9qMEZAugtnW] active_card=<alpha.AlphaObject[card]
...>, created=1324336085, description='Customer for My Test App',
livemode=False>"
val = s.split('[', 1)[1].split(']')[0]
Then we have:
>>> val
'cus_Y4o9qMEZAugtnW'
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:
[
matches a literal[
character(
begins a new group[A-Za-z0-9_]
is a character set matching any letter (capital or lower case), digit or underscore+
matches the preceding element (the character set) one or more times.)
ends the group]
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.
This should do the job:
re.match(r"[^[]*[([^]]*)]", yourstring).groups()[0]
You can also use
re.findall(r"[([A-Za-z0-9_]+)]", string)
if there are many occurrences that you would like to find.
See also for more info:
How can I find all matches to a regular expression in Python?
your_string = "lnfgbdgfi343456dsfidf[my data] ljfbgns47647jfbgfjbgskj"
your_string[your_string.find("[")+1 : your_string.find("]")]
courtesy: Regular expression to return text between parenthesis
You can use
import re
s = re.search(r"[.*?]", string)
if s:
print(s.group(0))
How about this ? Example illusrated using a file:
f = open('abc.log','r')
content = f.readlines()
for line in content:
m = re.search(r"[(.*?)]", line)
print m.group(1)
Hope this helps:
Magic regex : [(.*?)]
Explanation:
[
: [
is a meta char and needs to be escaped if you want to match it literally.
(.*?)
: match everything in a non-greedy way and capture it.
]
: ]
is a meta char and needs to be escaped if you want to match it literally.
This snippet should work too, but it will return any text enclosed within "[]"
re.findall(r"[([a-zA-Z0-9 ._]*)]", your_text)