Get the String Within Brackets in Python

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:

  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.

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 demo

To 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 possible
  • matchingString - a hardcoded string
  • [^)]* - zero or more chars other than )
  • \) - a ) char.

See the Python demo:

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 Demo

Your match is available in captured group #1

RegEx Details:

  • (?: ^data | (?!^)\G ): Start with data 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

Reference: RegEx Expression Recursion



Related Topics



Leave a reply



Submit