Asked  10 Months ago    Answers:  5   Viewed   3 times

I have a text file like -

{[a] abc (b(c)d)}

I want to remove the content between these bracket [] and (()). so the output should be -

 abc

I removed the content between parentheses but could not remove the content between this [] I have tried below code -

import re

with open('data.txt') as f:
    input = f.read()
    line = input.replace("{","")
    line = line.replace("}","")
    output = re.sub(r'(.*)', "", line)
    print output

The output is -

[a] abc

In my code first I replace the {} and then remove the content from () . I want to add [.*] in output = re.sub(r'(.*)', "", line) this line . But could not find a way to do this. I am still learning python. So I am facing this problem. please help.

 Answers

4

You may check if a string contains [, ], (<no_parentheses_here>) or [no_brackets_here] substrings and remove them while there is a match.

import re                                    # Use standard re
s='{[a] abc (b(c)d)}'
rx = re.compile(r'([^()]*)|[[^][]*]|[{}]')
while rx.search(s):                          # While regex matches the string
    s = rx.sub('', s)                        # Remove the matches
print(s.strip())                             # Strip whitespace and show the result
# => abc

See the Python demo

It will also work with paired nested (...) and [...], too.

Pattern details

  • ([^()]*) - (, then any 0+ chars other than ( and ), and then )
  • | - or
  • [[^][]*] - [, then any 0+ chars other than [ and ], and then ]
  • | - or
  • [{}] - a character class matching { or }.
Friday, August 20, 2021
 
Szenis
 
5

It works for me. You must be doing something wrong.

>>> re.match(r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', '127.255.0.0').groups()
('127', '255', '0', '0')

Don't forget to escape the regex using raw strings: r'regex_here' as stated in the Regex Howto

Saturday, May 29, 2021
 
fardjad
 
3
s/([^)]*)//

So in Python, you'd do:

re.sub(r'([^)]*)', '', filename)
Saturday, June 5, 2021
 
csi
 
csi
4

Have a look at re.sub:

result = re.sub(r"(d.*?)s(d.*?)", r"1 2", string1)

This is Python's regex substitution (replace) function. The replacement string can be filled with so-called backreferences (backslash, group number) which are replaced with what was matched by the groups. Groups are counted the same as by the group(...) function, i.e. starting from 1, from left to right, by opening parentheses.

Tuesday, June 8, 2021
 
Norgul
 
3

Not a regex based solution. But does the trick.

a='''S
LINC             SHORT LEGAL                                   TITLE NUMBER
0037 471 661     1720278;16;21                                 172 211 342

LEGAL DESCRIPTION
PLAN 1720278  
BLOCK 16  
LOT 21  
EXCEPTING THEREOUT ALL MINES AND MINERALS  

ESTATE: FEE SIMPLE  
ATS REFERENCE: 4;24;54;2;SW

MUNICIPALITY: CITY OF EDMONTON

REFERENCE NUMBER: 172 023 641 +71

---------------------------------------------------------------------------- 
----
            REGISTERED OWNER(S)
REGISTRATION    DATE(DMY)  DOCUMENT TYPE      VALUE           CONSIDERATION
--------------------------------------------------------------------------- 
-- 
---

172 211 342    15/08/2017  AFFIDAVIT OF                       CASH & MTGE'''

doc = (a.split('15/08/2017', 1)[1]).strip() 
# used split with two white spaces instead of one to get the desired result
print(doc.split("  ")[0].strip()) # outputs AFFIDAVIT OF
print(doc.split("  ")[-1].strip()) # outputs CASH & MTGE

Hope it helps.

Friday, July 30, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :