Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with regex

Status
Not open for further replies.

MikeM2468

IS-IT--Management
Apr 5, 2011
100
US
Since I couldn't find a Regex forum, I figure readers here would be able to help. I'm using Python scripts (PyWikipediabot) to make some updates to a private Wiki. One of the things I'm trying to do is to select all the text on a page and replace it with new content. The closest that I've gotten is with .*.$ but that selects everything after the last line break but nothing before.

Any tips would be appreciated.
 
First of all ".*" will find all the characters of any kind on the page.

But regexp for that is a bit of overkill. How are you saving the changes? Maybe just overwriting instead of appending will suffice.

_________________
Bob Rashkin
 
Thanks for the reply. I found that .*. worked best. But one thing that I had missed before was that I needed to tell it that . matched line breaks.
 
You don't need regualr expression if you want select all lines.
For example, if you have a file with
Code:
line 1
line 2
line 3
then something like this joins the lines together without new line characters
Code:
[COLOR=#a020f0]import[/color] string

f = [COLOR=#008080]open[/color]([COLOR=#ff00ff]"file.txt"[/color], [COLOR=#ff00ff]"r"[/color])
[COLOR=#0000ff]# list of lines[/color]
lines_lst = f.readlines()
[COLOR=#008080]print[/color] lines_lst

[COLOR=#0000ff]# string of lines[/color]
lines_str = string.join(lines_lst).replace([COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color],[COLOR=#ff00ff]'[/color])
[COLOR=#008080]print[/color] lines_str
Output:
Code:
['line 1\n', 'line 2\n', 'line 3\n']
line 1 line 2 line 3
 
Thanks. This is beyond what I need because I'm trying to stay away from editing the actual script. I just needed to pass regex to an existing python script. The method that I mentioned above worked for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top