473,662 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expressions Problem

Hi!

I'm trying to 'clean up' this source file using regular expressions
in Python. My problem is, that when I try to delete extra lines, my
code fails. Here's an example....

/**
*
* Project: MyProject
*
*
*
*
*
*
*
* Description:
*
* This file contains the some code.
*
* Public Functions:
*
* function_1
* function_2
*
* Private Functions:
*
* None.
*
*
* Notes:
*
* None.
*
*
*
*************** *************** *************** *************** *************/
......I would like my code to only have one * space between lines, and
not all that white space that I see there. I tried to use the regular
expression: '^\*\n$^\*\n$' but that does not work. I've tried a bunch
of things and none of them seem to work....please help!!! Thanks in
advance, Oriana
Jul 18 '05 #1
2 1795
On 2004-09-09, Oriana <or**********@t halesesec.com> wrote:
.....I would like my code to only have one * space between lines, and
not all that white space that I see there. I tried to use the regular
expression: '^\*\n$^\*\n$' but that does not work. I've tried a bunch
of things and none of them seem to work....please help!!! Thanks in
advance, Oriana


Hrm, some suggestions.

First, you need to set the MULTILINE mode on the regular expression
object. You can do this with re.compile(patt ern,re.MULTILIN E).

Secondly, the "$" character matches just before the newline. So it
should be '^\*$\n'. In MULTILINE mode

Third, the regex you have here will reduce two blank comment lines to
one.

Try this:
string = """* .... *
.... *
.... *
.... *
.... *
.... *
.... """ foo = re.compile(r'(^ \*\s*\n){2,}',r e.MULTILINE)
foo.sub("*\n",s tring)

'*\n'

The blank comment line is described by (^\*\s*\n) (asterisk at the start
of a line, followed by 0 or more space characters, then a newline).
The {2,} says "match two or more of this group."

Also, I can't really overrecommend "Mastering Regular Expressions" as a
good book for regular expression users:
http://www.oreilly.com/catalog/regex/

There is also a nice python-centric regex page at:
http://www.amk.ca/python/howto/regex/
--
Kirk Job-Sluder
"The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust." --Scary Go Round
Jul 18 '05 #2
Oriana wrote:
Hi!

I'm trying to 'clean up' this source file using regular expressions
in Python. My problem is, that when I try to delete extra lines, my
code fails. Here's an example....


You probably need the re.MULTILINE flag. This worked for me
import re
pat = re.compile(r"^\ *\s*\n(^\*\s*\n )+", re.MULTILINE)
text = """/** .... *
.... * Project: MyProject
.... *
.... *
.... *
.... *
.... *
.... *
.... *
.... * Description:
.... *
.... * This file contains the some code.
.... *
.... * Public Functions:
.... *
.... * function_1
.... * function_2
.... *
.... * Private Functions:
.... *
.... * None.
.... *
.... *
.... * Notes:
.... """ print pat.sub("*\n", text) /**
*
* Project: MyProject
*
* Description:
*
* This file contains the some code.
*
* Public Functions:
*
* function_1
* function_2
*
* Private Functions:
*
* None.
*
* Notes:


Andrew
da***@dalkescie ntific.com
Jul 18 '05 #3

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

Similar topics

8
2423
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average: 0.04, 0.02, 0.01" how can I extract this number with RE or otherwise?
11
3900
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category I am looking to retrive name value...
3
347
by: Mark | last post by:
I'm having trouble creating a regular expression to parse bits of data from a string and was hoping someone could lead me in the right direction. Consider the following string 423456 Victor Frankenstein, M.D. 04/04/200 I want to construct a new string to look like this 04/04/2004-423456-Frankenstei My biggest problem is getting the last name, this is because there may or may not be something between the first and last name (ie....
4
5167
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 adate LIKE "2004.01.10 __:15") .... into something like this: .... where adate LIKE "2004.01.10 __:(30/15)" ...
7
3818
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
9
3353
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use an app call The Regulator, which makes it pretty easy to build and test regular expressions. As a warning, I'm real weak with regular expressions. Let's say my regular expression is:
3
3326
by: LordHog | last post by:
Hello all, I am attempting to create a small scripting application to be used during testing. I extract the commands from the script file I was going to tokenize the each line as one of the requirements is there one command per line. I have always wanted to learn Regular Expressions, so I was hoping I might do this using Regular Expressions. For a fair number of the command will have the syntax like Write( 0x123, 0x12, 25, 100 ) <-...
25
5147
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
5
2990
by: teo | last post by:
I need to implement a boolean evaluation in a Regular Expression like this: (aaa AND bbb) OR (ccc AND ddd) (see the #3 case) - - - 1) If I need to match a single word only,
12
2462
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 http://en.wikipedia.org/wiki/Regular_expression http://www.regular-expressions.info/javascript.html http://www.webreference.com/js/column5/
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.