473,396 Members | 1,866 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,396 software developers and data experts.

Negation in regular expressions

It's always striked me as odd that you can express negation of a single
character in regexps, but not any more complex expression. Is there a
general way around this shortcoming ? Here's an example to illustrate a
use case:
>>import re
# split with '@' as delimiter
>>[g.group() for g in re.finditer('[^@]+', 'This @ is a @ test ')]
['This ', ' is a ', ' test ']

Is it possible to use finditer to split the string if the delimiter was
more than one char long (say 'XYZ') ? [yes, I'm aware of re.split, but
that's not the point; this is just an example. Besides re.split returns
a list, not an iterator]

George

Sep 8 '06 #1
6 4970

George Sakkis wrote:
It's always striked me as odd that you can express negation of a single
character in regexps, but not any more complex expression. Is there a
general way around this shortcoming ? Here's an example to illustrate a
use case:
>import re
# split with '@' as delimiter
>[g.group() for g in re.finditer('[^@]+', 'This @ is a @ test ')]
['This ', ' is a ', ' test ']

Is it possible to use finditer to split the string if the delimiter was
more than one char long (say 'XYZ') ? [yes, I'm aware of re.split, but
that's not the point; this is just an example. Besides re.split returns
a list, not an iterator]

George
If your wiling to use groups then the following will split
>>[g.group(1) for g in re.finditer(r'(.+?)(?:@#|$)', 'This @# is a @# test ')]
['This ', ' is a ', ' test ']

- Paddy.

Sep 8 '06 #2

Paddy wrote:
George Sakkis wrote:
It's always striked me as odd that you can express negation of a single
character in regexps, but not any more complex expression. Is there a
general way around this shortcoming ? Here's an example to illustrate a
use case:
>>import re
# split with '@' as delimiter
>>[g.group() for g in re.finditer('[^@]+', 'This @ is a @ test ')]
['This ', ' is a ', ' test ']

Is it possible to use finditer to split the string if the delimiter was
more than one char long (say 'XYZ') ? [yes, I'm aware of re.split, but
that's not the point; this is just an example. Besides re.split returns
a list, not an iterator]

George

If your wiling to use groups then the following will split
>[g.group(1) for g in re.finditer(r'(.+?)(?:@#|$)', 'This @# is a @# test ')]
['This ', ' is a ', ' test ']

- Paddy.
Here is another wrapping of the same finditer call that just allows you
to call .group() on the result
>>class G(object):
.... def __init__(self, x):
.... def grp(x=x):
.... return x
.... self.group = grp
....
>>[g.group() for g in (G(g.group(1)) for g in re.finditer(r'(.+?)(?:@#|$)', 'This @# is a @# test '))]
['This ', ' is a ', ' test ']
>>>
- Paddy.

Sep 8 '06 #3
George Sakkis wrote:
It's always striked me as odd that you can express negation of a single
character in regexps, but not any more complex expression. Is there a
general way around this shortcoming ? Here's an example to illustrate a
use case:

>>>>import re

# split with '@' as delimiter
>>>>[g.group() for g in re.finditer('[^@]+', 'This @ is a @ test ')]

['This ', ' is a ', ' test ']

Is it possible to use finditer to split the string if the delimiter was
more than one char long (say 'XYZ') ? [yes, I'm aware of re.split, but
that's not the point; this is just an example. Besides re.split returns
a list, not an iterator]
I think you are looking for "negative lookahead assertions". See the docs.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 8 '06 #4
Ant

Steve Holden wrote:
George Sakkis wrote:
It's always striked me as odd that you can express negation of a single
character in regexps, but not any more complex expression. Is there a
general way around this shortcoming ?
The whole point of regexes is that they define expressions to match
things. [^x] doesn't express the negation of x, it is shorthand for
[a-wy-z...]. But the intent is still to match something. What you seem
to want is a way of saying "Match anything that doesn't match the
string 'XYZ' (for example)" What do you expect to get back from this?
In the string "abcd XYZ hhh XYZ" for example, "XYZ h" doesn't match
"XYZ", nor does the empty string, nor does the entire string.
I think you are looking for "negative lookahead assertions". See the docs.
Negative lookahead and lookbehind are great for expressing that you
want to match X as long as it isn't followed by Y ( "X(?!Y)" ) but
won't help much in your finditer example.

Is there a particular reason you don't want to use split?

Sep 8 '06 #5
Paddy wrote:
George Sakkis wrote:
It's always striked me as odd that you can express negation of a single
character in regexps, but not any more complex expression. Is there a
general way around this shortcoming ? Here's an example to illustrate a
use case:
>>import re
# split with '@' as delimiter
>>[g.group() for g in re.finditer('[^@]+', 'This @ is a @ test ')]
['This ', ' is a ', ' test ']

Is it possible to use finditer to split the string if the delimiter was
more than one char long (say 'XYZ') ? [yes, I'm aware of re.split, but
that's not the point; this is just an example. Besides re.split returns
a list, not an iterator]

George

If your wiling to use groups then the following will split
>[g.group(1) for g in re.finditer(r'(.+?)(?:@#|$)', 'This @# is a @# test ')]
['This ', ' is a ', ' test ']
Nice! This covers the most common case, that is non-consecutive
delimiters in the middle of the string. There are three edge cases:
consecutive delimiters, delimiter(s) in the beginning and delimiter(s)
in the end.

The regexp r'(.*?)(?:@#|$)' would match re.split's behavior if it
wasn't for the last empty string it returns:
>>s = '@# This @# is a @#@# test '
re.split(r'@#', s)
['', ' This ', ' is a ', '', ' test ']
>>[g.group(1) for g in re.finditer(r'(.*?)(?:@#|$)', s)]
['', ' This ', ' is a ', '', ' test ', '']

Any ideas ?

George

Sep 8 '06 #6
Ant
>re.split(r'@#', s)
['', ' This ', ' is a ', '', ' test ']
>[g.group(1) for g in re.finditer(r'(.*?)(?:@#|$)', s)]
['', ' This ', ' is a ', '', ' test ', '']
If it's duplicating the behaviour of split, but returning an iterator
instead, how about avoiding hacking around with messy regexes and use
something like the following generator:

def splititer(pattern, string):
posn = 0
while True:
m = pattern.search(string, posn)
if not m:
break
yield string[posn:m.start()]
posn = m.end()

Sep 8 '06 #7

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

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
6
by: sk.rasheedfarhan | last post by:
Hi , I am using regular expression in C++ code, . Negation is not working in the down loaded code. matches all characters except "a", "b", and "c] So I am in dilemma can negation work in C++...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.