regex


Python & Regex: Match all, but not when x or y


I have lines like this one:
Test, Customer Relationship (CR), 4D-, 1D-, 3D-magnetic (circuit, simulation) Edyson, test
In these lines, I want to match every comma (,) with some exceptions: I don't want to match a comma preceeded by a minus (-,) or if it inside Brackets (... , ...).
So in the example line, only the first, the second, and the last comma should be a match.
I know how to match comma not preceeded by a minus:
(?<!\-)\,
But I don't know how to match only commas that are not between brackets, and I don't know how to combine the rules.
Can this be done in regex? Thanks!
You can use this regex:
r'(?<!-),(?![^()]*\))'
(?<!-) is a negative lookbehind to avoid matching comma when preceded by hyphen
(?![^()]*\)) is a negative lookahead to avoid matching comma when it is with (...)
This is assuming you don't have unbalanced (...) text.
RegEx Demo
Example:
>>> s = 'Test, Customer Relationship (CR), 4D-, 1D-, 3D-magnetic (circuit, simulation) Edyson, test'
>>> print re.sub(r'(?<!-),(?![^()]*\))', '\n', s)
Test
Customer Relationship (CR)
4D-, 1D-, 3D-magnetic (circuit, simulation) Edyson
test
With the last version of the regex module:
import regex
s=' Test, Customer Relationship (CR), 4D-, 1D-, 3D-magnetic (circuit, simulation) Edyson, test'
reg = regex.compile(r'(?:\([^)]*\)|-,)(*SKIP)(*F)|,')
print(reg.split(s))

Related Links

Trouble with regex in Perl
Duplicate symbols in regular expressions
NSRegularExpression to remove the rest codes
Regex replace in sublimetext2
Regular Expression Search Replace all non leading tabs with single space Notepad++
Django regular expression on success format
Reg ex required for finding two ||
regex from first character to the end of the string
How can I recognize the following group of strings using a grammar or regex?
htaccess redirect begins with
grep through binary file
Printing Text on Perl with RegEp
Regexp for substitution of anything other than numbers and 'N/A'
This regex is inefficient. Why does it crash Sublime Text 2's stack?
Posix regex in Postgresql to extract from quoted text?
Regular expression sequence matching

Categories

HOME
mongodb
sqlite
clojure
identityserver4
elf
serial-port
tags
android-sqlite
direction
android-wear
prebuild
image-segmentation
tsql
ibm-midrange
android-bluetooth
whitespace
build.gradle
r-caret
base
drag
intl
eps
gcov
research
vulkan
union
resolve
facebook-opengraph
accumulo
countdown
pywin32
textmatebundles
teamcity-9.0
dotnetbar
similarity
datamatrix
cadvisor
burp
confidence-interval
nested-loops
photo-gallery
google-shared-contacts
btle
tern
hostapd
uservoice
wrk
multisite
recurrence
replaygain
libev
g-wan
haste
lcov
masspay
android-sdk-tools
cakephp-2.3
xendesktop
webpagetest
rsh
groupbox
jongo
flotr2
gcloud-java
docklight
yapdatabase
multer
coldfusion-10
spl
cocoon-gem
oboe.js
heroku-toolbelt
reactive-cocoa-3
applicationstate
mysql5
jasmin
bitrock
httpservice
master-theorem
youtrack-api
yourls
lightstreamer
cfile
disabled-input
sshj
scrollpane
geometry-surface
route-me
eject
jcycle
blackberry-cascades
doophp
vote
cocosbuilder
qbwc
nextpeer
xml-dtd
server.mappath
bapi
soapheader
plinqo
wiki-markup
msn-messenger
cracker
clipboard-pictures
newsgroup
large-teams

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App