Get the string within brackets in Python
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
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. Regular expression to return text between parenthesis
If your problem is really just this simple, you don't need regex:
s[s.find("(")+1:s.find(")")]
Extract string between two brackets, including nested brackets in python
>>> import re
>>> s = """res = sqr(if((a>b)&(a<c),(a+b)*c,(a-b)*c)+if()+if()...)"""
>>> re.findall(r'if\((?:[^()]*|\([^()]*\))*\)', s)
['if((a>b)&(a<c),(a+b)*c,(a-b)*c)', 'if()', 'if()']
For such patterns, better to use VERBOSE
flag:>>> lvl2 = re.compile('''
... if\( #literal if(
... (?: #start of non-capturing group
... [^()]* #non-parentheses characters
... | #OR
... \([^()]*\) #non-nested pair of parentheses
... )* #end of non-capturing group, 0 or more times
... \) #literal )
... ''', flags=re.X)
>>> re.findall(lvl2, s)
['if((a>b)&(a<c),(a+b)*c,(a-b)*c)', 'if()', 'if()']
To match any number of nested pairs, you can use regex module, see Recursive Regular Expressions
How to get Data between the brackets in string in python?
import re
def extract_items(s):
return re.compile("(?<=[^\(]')\w+").findall(s)
mylist = ["Fruits in ['Apples', 'Mangoes']","Vegetables in ['Carrots', 'Onion']"]
list1 = list(map(extract_items, mylist))
print(list1)
Get string inside brackets
You may use your own pattern in re.findall
to grab all the contents inside [...]
:
import re
var = 'u16 arrayName_1[8][7]'
index = re.findall(r'\[(.*?)\]', var)
print(index) # => ['8', '7']
See Python demoTo only match digits inside, use \[([0-9]+)]
regex. Also, you do not have to escape the ]
symbol outside the character class, and you should consider using raw string literals to define your regex patterns to avoid confusion with unescaped backslashes.
Regular Expression that return matches specific strings in bracket and return its next and preceding string in brackets
You can use
\([^()]*?matchingString[^)]*\)
See the regex demo. Due to the [^()]*?
, the match will never overflow across other (...)
substrings.Regex details:
\(
- a(
char[^()]*?
- zero or more chars other than(
and)
as few as possiblematchingString
- a hardcoded string[^)]*
- zero or more chars other than)
\)
- a)
char.
import re
text = 'This is an sample string which have some information in brackets (info; matchingString, someotherString).'
regex= r"\([^()]*?matchingString[^)]*\)"
print( re.findall(regex, text) )
# => ['(info; matchingString, someotherString)']
Get the string within brackets and remove useless string in Python
This works, using split and strip:
'0x69313430303239377678(i1400297vx)'.split('(')[1].strip(')')
but a regex would be more readable! Regex for fetching multiple strings within brackets
In PCRE you can use this recursive regex to capture what you want:
~(?: ^data | (?!^)\G ) \h+ ( \w+ \h* ( \( (?: [^()]*+ | (?-1) )* \) ) )~xi
RegEx DemoYour match is available in captured group #1
RegEx Details:
(?: ^data | (?!^)\G )
: Start withdata
in a line or else match from end of previous match i.e.\G
\h+
: Match 1+ whitespaces(
: Start capture group #1\w+
: Match 1+ word characters\h*
: Match 0+ whitespaces(
: Start capture group #2\(
: Match literal(
(opening)(?:
: Start non-capture group[^()]*+
: Match 0 or more of any characters that are not(
and)
|
: OR(?-1)
: Recurse the match with latest group i.e. #2
)*
: End non-capture group. Match 0 or more of this group
)
: capture group #2
)
: capture group #1
Related Topics
Slicing of a Numpy 2D Array, or How to Extract an Mxm Submatrix from an Nxn Array (N>M)
How to Get Windows' Special Folders for Currently Logged-In User
How to Suppress or Capture the Output of Subprocess.Run()
How to Upsert Pandas Dataframe to Microsoft SQL Server Table
Python & Selenium: Difference Between Driver.Implicitly_Wait() and Time.Sleep()
Count the Number of Occurrences of a Certain Value in a Dictionary in Python
Lookup Values by Corresponding Column Header in Pandas 1.2.0 or Newer
Scale Matplotlib.Pyplot.Axes.Scatter Markersize by X-Scale
Multiprocessing:Use Tqdm to Display a Progress Bar
How to Check Task Status in Celery
What Are All the Dtypes That Pandas Recognizes
Looping from 1 to Infinity in Python
Meaning of Using Commas and Underscores with Python Assignment Operator