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

Problems with regexps

I'm writing a program to scan through a bunch of VB source.**An*example*of
one of the types of strings I'm trying to match is:

****response.write*Request.Cookies*("domain")("cna me")

but I do not want to match variable assignments like this (they are picked
up later by a different pattern):

****strXRSCust=*Request.Cookies*("domain")("cname" )

I'm differentiating between the two forms by looking for an equal sign
followed by zero or more spaces; if the '=' is there, then I don't want to
match.**Here's*where*it*gets*weird.**This*pattern* works*perfectly*(as*long
as there are 1 or more spaces after the '='), in that it will not match
the assignment example above:

****re.compile(r'(?<!=)\s+Request.Cookies\s*((\(\s *".*?"\s*\)\s*)+)')

I really want to use the pattern below to match for zero or more spaces
(not one or more).**Note*that*it's*identical*except*that*the*f irst*'\s+'
is replaced with a '\s*':

****re.compile(r'(?<!=)\s*Request.Cookies\s*((\(\s *".*?"\s*\)\s*)+)')

I don't know why, but the second pattern does match the assignment example
above, although I don't think it should.

It seems like there's a problem with the negative lookbehind assertion and
that the variable-length '\s' pattern immediately following it is throwing
it off.**Any*thoughts?
--
Kirk Strauser
The Day Companies
Jul 18 '05 #1
6 1581
OK, let me give a clearer demonstration. Given this program:

#!/usr/bin/env python

import re

sample = 'FOO= BAR'

if re.search(r'[^=]\s+BAR', sample):
print 'Match 1.'

if re.search(r'[^=]\s*BAR', sample):
print 'Match 2.'

When run, it produces "Match 2.". Why? I want to match:

1) Anything but '=', followed by
2) Zero or more spaces, followed by
3) 'BAR'

The first pattern correct does *not* match. Why doesn't the '= ' get struck
down by the '[^=]' pattern before a '\s+' but not before '\s*'?

Thanks,
--
Kirk Strauser
The Day Companies
Jul 18 '05 #2
> sample = 'FOO= BAR'
if re.search(r'[^=]\s*BAR', sample):
print 'Match 2.'


The [^=] will match on the space preceding BAR. The \s* matches because
there are zero spaces remaining. Result: match.

Prediction: you can get the first expression to fail by putting in two
spaces, not one.

Caveat: untested.
--

Dennis Reinhardt

De*****@dair.com http://www.spamai.com?ng_py
Jul 18 '05 #3
On Fri, 07 Nov 2003 23:00:34 GMT, Kirk Strauser wrote:
sample = 'FOO= BAR'

if re.search(r'[^=]\s+BAR', sample):
print 'Match 1.'

if re.search(r'[^=]\s*BAR', sample):
print 'Match 2.'

When run, it produces "Match 2.". Why? I want to match:

1) Anything but '=', followed by
2) Zero or more spaces, followed by
3) 'BAR'


To understand the answer, you must understand the question.

If you inspect the MatchObject returned from re.search(), you'll learn
more about what's going on:
import re
sample = 'FOO= BAR'
match = re.search( r'[^=]\s+BAR', sample )
match
The first match returns the None object.
match = re.search( r'[^=]\s*BAR', sample )
match <_sre.SRE_Match object at 0x4021b090>

The second match returns a Match object; let's see what it matched.
match.group(0) ' BAR'


The matching string contains the space ("Anything but '='"), followed by
nothing ("Zero or more spaces"), followed by BAR. Exactly what you
asked for.

--
\ "It is seldom that liberty of any kind is lost all at once." |
`\ -- David Hume |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #4
On Fri, 07 Nov 2003 21:20:08 GMT, Kirk Strauser wrote:
I'm writing a program to scan through a bunch of VB source.**An*example*of
one of the types of strings I'm trying to match is: ****response.write*Request.Cookies*("domain")("cna me") but I do not want to match variable assignments like this (they are picked
up later by a different pattern): ****strXRSCust=*Request.Cookies*("domain")("cname" )


Try http://weitz.de/regex-coach/

--
Cogito ergo I'm right and you're wrong. -- Blair Houghton

(setq reply-to
(concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz>"))
Jul 18 '05 #5
I've just added script-based find-changed commands to Leo 4.1 beta 1. So
Leo can execute a script to find the next match, and optionally executes a
script to do the replacement. This gives Leo the full power of languages
like Icon or Snobol, using the plain Python language. There is a dead
simple way for the find script to communicate with the change script, and
for the find script to continue or not in the case of the Find All or Change
All commands.

BTW, scripts could search anywhere, not just in a Leo outline. For
example, the find script could pull in the found text from any file to any
Leo node (creating an outline node if you like). Etc.

I'm very excited about this new feature. At long last we are not limited to
non-extensible, wimpy tools like re. Fully interactive if you want; fully
batch if you want. Undo is easy, etc. The sky is the limit, and when you
are done, you will be able to understand the scripts you wrote. End of re.
Yeah!

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #6
Actually, I misspoke a little. Script-find is fully compatible with re,
because re can appear in any script. Anyway, there is nothing particularly
wrong with re for simple tasks. So it's not the end of re; it's the end of
re for complex tasks for which re just isn't suited.

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #7

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

Similar topics

0
by: R. Tarazi | last post by:
Hello together, I'm having extreme difficulties using RegExps for a specific problem and would really appreciate any help and hope somebody will read through my "long" posting... 1. <?php...
5
by: Klaus Alexander Seistrup | last post by:
Hi, Is there a way to "expand" simple regexps? Something along the lines of: #v+ >>> rx = '(a|b)c?(d|f)' >>> expand_regexp(rx)
4
by: vivek | last post by:
Hi all, Here we are conducting a QIP program for teachers in other colleges. I am assigned the job of taking lectures in Python. Although there will be not much time (exactly speaking : 4 hours)...
4
by: Magnus Lie Hetland | last post by:
Hi! I've been looking at ways of dealing with nested structures in regexps (becuase I figured that would be faster than the Python parsing code I've currently got) and came across a few...
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
1
by: 3f | last post by:
Hello; We have made a web application that people can download from our web site and installed on: Windows XP Windows 2000 Professional Windows 2003 Server Windows 2000 Server
5
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) -...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
0
by: Sergistm | last post by:
Hello World, :D I have a problem that it is making me crazy, I hope you can help me. I'm trying to execute a .exe file with the Procces.Start, and there is no problem when the file is on my...
4
by: possibilitybox | last post by:
I'm trying to make a unicode friendly regexp to grab sentences reasonably reliably for as many unicode languages as possible, focusing on european languages first, hence it'd be useful to be able...
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
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: 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:
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.