excluding search string in regular expressions
Bengt Richter
bokr at oz.net
Thu Oct 21 18:38:00 EDT 2004
On Thu, 21 Oct 2004 13:36:46 +0200, Franz Steinhaeusler <franz.steinhaeusler at utanet.at> wrote:
>Hello,
>
>Following Problem:
>
>find only occurances, where in the line are'::' characters and
>the former line is not equal '**/'
>
>so 2) and 3) should be found and 1) not.
>
>1)
>"""
>**/
>void C::B
>"""
>
>2)
>"""
>
>void C::B
>"""
>
>3)
>"""
>*/
>void C::B
>"""
>
>I tried something
>"\*\*/\n.*::"
>
>But this is the opposite.
>
>So my question is: how can I exclude a pattern?
>
>single characters with [^ab] but I need not(ab)
>
>not_this_brace_pattern(\*\*/\n).*::
>
>thank you in advance,
To look back a line, I think I'd just use a generator, and test current
and last lines to get what I wanted. E.g., perhaps you can adapt this:
(I am just going literally by
"""
find only occurances, where in the line are'::' characters and
the former line is not equal '**/'
"""
which doesn't need a regex)
>>> def findem(lineseq):
... getline = iter(lineseq).next
... curr = getline().rstrip()
... while True:
... last, curr = curr, getline().rstrip()
... if '::' in curr and last != '**/': yield curr
...
I made a file, modifying your data a little:
>>> print '----\n%s----'% file('franz.txt').read()
----
1)
"""
**/
void C::B -- no (1)
"""
2)
"""
void C::B -- yes (2)
"""
3)
"""
*/
void C::B -- yes (3)
"""
----
Here's what the generator returns:
>>> for line in findem(file('franz.txt')): print repr(line)
...
'void C::B -- yes (2)'
'void C::B -- yes (3)'
Regards,
Bengt Richter
More information about the Python-list
mailing list