473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

RegEx for matching brackets

Below is a (flawed) one line RegEx that checks curly brackets (from
awk/c/python input) are being matched. Is there a one liner for doing
this in python?

ThanX
N

re_open_close="(((\{))[^{}]*((?(0)\})))+"
re_open_close=re.compile(re_open_close)
tests="""
{ this is a test BAD
{ this is a test } OK
{ this is a test } { this is a test } OK
{ this is a test } { this { this is a test } is a test } OK
{ this is a test { this { this is a test } is a test } missing
close BAD
""".splitlines()[1:]
for test in tests:
if bool(re_open_close.search(test)) == bool(re.search("OK",test)):
print "DETECTED:",test
else:
print "MISSED:",test
[linux]$ python ./re_matching.py
DETECTED: { this is a test BAD
DETECTED: { this is a test } OK
DETECTED: { this is a test } { this is a test } OK
DETECTED: { this is a test } { this { this is a test } is a test }
OK
MISSED: { this is a test { this { this is a test } is a test }
missing close BAD
Jun 27 '08 #1
4 5875
On May 2, 9:44 am, NevilleDNZ <neville...@gmail.comwrote:
Below is a (flawed) one line RegEx that checks curly brackets (from
awk/c/python input) are being matched. Is there a one liner for doing
this in python?

ThanX
N

re_open_close="(((\{))[^{}]*((?(0)\})))+"
re_open_close=re.compile(re_open_close)
tests="""
{ this is a test BAD
{ this is a test } OK
{ this is a test } { this is a test } OK
{ this is a test } { this { this is a test } is a test } OK
{ this is a test { this { this is a test } is a test } missing
close BAD
""".splitlines()[1:]
for test in tests:
if bool(re_open_close.search(test)) == bool(re.search("OK",test)):
print "DETECTED:",test
else:
print "MISSED:",test
[linux]$ python ./re_matching.py
DETECTED: { this is a test BAD
DETECTED: { this is a test } OK
DETECTED: { this is a test } { this is a test } OK
DETECTED: { this is a test } { this { this is a test } is a test }
OK
MISSED: { this is a test { this { this is a test } is a test }
missing close BAD
Regexes can't count.

To check for balanced braces, you need to write code e.g.

# untested
def balanced_braces(s):
n = 0
for c in s:
if c == '{':
n += 1
elif c == '}':
n -= 1
if n < 0:
return False
return n == 0

HTH,
John
Jun 27 '08 #2
On May 1, 7:44 pm, NevilleDNZ <neville...@gmail.comwrote:
Below is a (flawed) one line RegEx that checks curly brackets (from
awk/c/python input) are being matched. Is there a one liner for doing
this in python?
There is not even a 1000-liner regular expression for this; it's a
context-free language [1], not a regular one [2]. Either do it
manually or use a parser generator [3].

George

[1] http://en.wikipedia.org/wiki/Context-free_language
[2] http://en.wikipedia.org/wiki/Regular_language
[3] http://wiki.python.org/moin/LanguageParsing
Jun 27 '08 #3
On May 2, 11:13 am, George Sakkis <george.sak...@gmail.comwrote:
[1]http://en.wikipedia.org/wiki/Context-free_language
[2]http://en.wikipedia.org/wiki/Regular_language
[3]http://wiki.python.org/moin/LanguageParsing
Thanx for the link to these parsers. ANTLR looks interesting.
Yoyo: http://www-users.cs.york.ac.uk/~fish...yoyovwg/readme

I figured out a way to do it in python.

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import re
tests="""
{ a test BAD
{ a test } OK
{ a test } { a test } OK
{ a test } { this { a test } is a test } OK
{ a test { this { a test } is a test } missing close BAD
a test } { this { a test } is a test } BAD
{ a test } this { a test } is a test } BAD
""".splitlines()[1:]

def referee(str):
return bool(re.search("OK",test))

def check_open_close(str):
try:
eval("".join({"{":"[","}":"],"}[c] for c in re.findall( "([{}])|(?:
[^{}]+)", str) if c))
except SyntaxError:
return False
else:
return True

for test in tests:
if check_open_close(test) == referee(test):
print "DETECTED:",test
else:
print "MISSED:",test
[linux]$ ./matchall_open_close.py
DETECTED: { a test BAD
DETECTED: { a test } OK
DETECTED: { a test } { a test } OK
DETECTED: { a test } { this { a test } is a test } OK
DETECTED: { a test { this { a test } is a test } missing close BAD
DETECTED: a test } { this { a test } is a test } BAD
DETECTED: { a test } this { a test } is a test } BAD

It's not exactly what I planned, but as a python one/two-liner it
works fine.
eval("".join({"{":"[","}":"],"}[c] for c in re.findall( "([{}])|(?:
[^{}]+)", str) if c))

ThanX again
N
Jun 27 '08 #4
To check a complete python expression use:

def check_open_close(expr):
try:
eval(expr)
except SyntaxError:
return False
else:
return True

This also ignores brackets in quotes, and checks <= & >= operators are
syntatically correct etc...
But is may have side effects... ;-)
eg.
check_open_close('__import__("os").system("echo rm -rf /") # OK')

c.f http://en.wikipedia.org/wiki/Scope_creep

N
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aeuglein | last post by:
Hello! I have this RegEx: /(+:\/\/+)/i Now, I want to exlude on the end of a String the formats .gif / .jpg / ..png / .exe / .zip / .rar How I can this add to my regex ?
9
by: William Ryan | last post by:
How do I ignore something that's part of what I want to find. For instance..I have this string I have a regex \]\d*\] to find the whole thing. However, I don't want to have either of the...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
6
by: Nurchi BECHED | last post by:
I have a filename and its process id in brackets. The problem is, the filename can contain brackets and numbers in it, but the last number in the brackets is always the process id. Now, assume,...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
2
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the...
9
by: Simon Woods | last post by:
Hi I'm new to Regular Expressions so ... I trying to work out regular expressions to parse the following (a + (b + c)) I really want to replace it with
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.