Apolyton Archive  |  Preserved copy of the Apolyton Civilization Site and its forums as they stood in September 2005. Read-only; nothing here can be posted to or replied to.  |  Forum index |  About this archive |  The 1998–2001 UBB forums
Today on Apolyton WARDELL INTERVIEW PROMO A.C.S. HISTORY CHAPTER 4 GET CIV4 /w FREE PLUS! A.C.S. PHOTO GALLERY GET A.O.M. V1.1
Apolyton Civilization Forums
main| civ2| civ3| civ4| smac| ctp2| ron| moo3| galciv| galciv2| alt| about|
ApolytonPLUS | register | search | faq | new posts | pm (-/-) | upload | members
hall of fame new! | civgroups | civgroups news | interviews | the column | radio | chat | directory | news | store | PLUS
Apolyton Civilization Forums : Powered by vBulletin version 2.0.3 Apolyton Civilization Forums > Miscellaneous > Off-Topic > Help! with regular expressions
Show a Printable Version | Email This Page to Someone! | Receive updates to this thread | Report this to Apolyton news!
20.Sep: FOURTH CHAPTER IN `HISTORY OF...` PUBLISHED
13.Sep: NEWS ON THE FLY
06.Sep: APOLYTON DONATES TO HURRICANE RELIEF

bottom of page
   - CIVILIZATION 3 $9.99 - CIVILIZATION 2 from $6.95 - CIVILIZATION 2 from $4.25 - ALPHA CENTAURI + ALIEN CROSSFIRE (laptop collection) from $19.99 - ALPHA CENTAURI PC $9.99 -->
Author
Thread    < Last Thread     Next Thread > Post New Thread     Post A Reply
MattH is offline MattH
King
Go sneer at that cow creamer!
Jul 2002
time: 05:20
  Old Post 30-06-2005 21:56
Edit/Delete Message Reply w/Quote
#1 Report this post to a moderator
Help! with regular expressions Support Apolyton buy from Amazon

Okay, so the situation here is that I have about 2100 lines of code like this:

code:
(tr) (td colspan=2 height="84" class="body")Badgers!(/td) (/tr)


And I need to run a find-and-replace to transform it into code like this:

code:
(p)Badgers!(/p)


The problem is there's no way to isolate the "Badgers!" string without regular expressions... and I have no clue how to build 'em. Can anyone help?

Mercator is offline Mercator
King
Sorekara no Nanimo
Jan 1970
time: 06:20
  Old Post 01-07-2005 02:18 Visit Mercator's homepage!
Edit/Delete Message Reply w/Quote
#2 Report this post to a moderator
Got spare money?

I take it you have the software needed to use them? Or is that what the problem is?

The following Perl one-liner ought to work, depending on how exact that description of the data really was. It won't work if the stuff between the tags stretches across multiple lines, for instance.

code:
perl -0pi'.bak' -e "s/<tr>\n\s*<td.*?>(.*)<\/td>\n\s*<\/tr>/<p>$1<\/p>/gi;" filename


And if you want to know what all that means...

The -0pi'.bak' -e are command-line switches:
  • The p will let Perl read a "record", execute the Perl code on it and write the result back.
  • The i'.bak' means that a backup is made of the original file called filename.bak.
  • The 0 makes Perl read in the contents of the entire file at once. To be more specific, it sets the record separator to the null character.
    The default record separator is the newline. But since the pattern stretches across multiple lines, it would never match if it only ever gets part of the pattern at a time.
    If your file is really big, this might not be a good idea.
  • The e means Perl will execute the code mentioned after it on the command-line.
  • The s///gi; is the regular expression. Replacing the part between the first two / with the part between the second two /. The g makes it replace all occurences. the i makes it match case-insensitively.


So as far as the regular expression is concerned:
  • \n matches a newline
  • \s* matches zero or more whitespace characters
  • .*? matches as few characters as needed (*?) of any (.) character to make the entire expression match.
  • the parenthesised (.*) is the part you need. The $1 in the replacement part corresponds to this.


Right...

Bill3000 is offline Bill3000
King
of Soloralism
Jul 1999
time: 00:20
  Old Post 01-07-2005 02:33 Visit Bill3000's homepage!
Edit/Delete Message Reply w/Quote
#3 Report this post to a moderator
Enter the AD-FREE zone

Add some mushrooms and a snake, and you'll be fine.

MattH is offline MattH
King
Go sneer at that cow creamer!
Jul 2002
time: 05:20
  Old Post 01-07-2005 02:51
Edit/Delete Message Reply w/Quote
#4 Report this post to a moderator
Support Apolyton, buy Galactic Civilizations: Deluxe Edition

Thanks, I'll see if I can't fudge that into working.

I'm using a text editor that accepts regular expressions for searches, find & replace. But I'm willing to run the file through whatever to lessen my workload

Mercator is offline Mercator
King
Sorekara no Nanimo
Jan 1970
time: 06:20
  Old Post 01-07-2005 05:44 Visit Mercator's homepage!
Edit/Delete Message Reply w/Quote
#5 Report this post to a moderator
Support Apolyton or Terrorists Win

As long as it properly supports regular expressions, that text editor should work. Just use the two parts of the regular expressions

St Leo is offline St Leo
King of Scenario League
Member of the Apolyton Social Democratic Party
Jul 2005
time: 00:20
  Old Post 01-07-2005 08:50 Visit St Leo<br><img src=/forums/images/staff-icon.gif>'s homepage!
Edit/Delete Message Reply w/Quote
#6 Report this post to a moderator
Support Apolyton, buy Civilization III: Complete

You may be over-engineering this.

You can just replace (tr) with nothing, (/tr) with nothing, (td colspan=2 height="84" class="body") with (p), and (/td) with (/p).

MattH is offline MattH
King
Go sneer at that cow creamer!
Jul 2002
time: 05:20
  Old Post 01-07-2005 08:54
Edit/Delete Message Reply w/Quote
#7 Report this post to a moderator
Suffering from ads?

I thought of that solution too but the problem is there are other (td)s that need to be marked up as (/address)

Urban Ranger is offline Urban Ranger
Apolyton Duke of Off-Topic

Donate to the Red Cross
The City State of Noosphere, CPA special envoy
May 1999
time: 13:20
  Old Post 01-07-2005 09:35
Edit/Delete Message Reply w/Quote
#8 Report this post to a moderator
Increase the size of your Attachments

I would look for something like:

quote:

bol, followed by zero or more white spaces, followed by <tr>, followed by zero or more white spaces and then eol, then bol, zero or more white spaces, <td colspan=2 height="84" class="body"><Badgers!</td>, zero or more white spaces, eol, bol, zero or more white spaces, </tr>,zero or more white spaces, eol


I may have missed a number of "zero or more white spaces" somewhere.

OzzyKP is offline OzzyKP
King
Rockville, MD
Oct 1999
time: 00:20
  Old Post 01-07-2005 09:57 Visit OzzyKP's homepage!
Edit/Delete Message Reply w/Quote
#9 Report this post to a moderator
Support Apolyton buy from Amazon

quote:
Help! with regular expressions


<------------- Happy expression

<------------ Sad expression

<---------------- Mad expression

  < Last Thread     Next Thread > Post New Thread     Post A Reply
All times are GMT. The time now is 05:20.
Apolyton Time is 00:20.
    top of page
Rate This Thread:
Forum Jump:
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is ON
vB code is ON
Smilies are ON
[IMG] code is ON
 




Contact Us - Apolyton Civilization Site - Support Us!

Building a better Apolyton through better information. Click here and take our poll!
Non-US visitors, click here!

Powered by: vBulletin Version 2.0.3
Copyright ©2000, 2001, Jelsoft Enterprises Limited.

Page generated in 0.0389 seconds (90.74% PHP - 9.26% MySQL) with 30 queries
Page Loading Time:

Support Apolyton: Amazon USA | Amazon UK | Amazon DE | Amazon FR |
Support Apolyton and get FREE PLUS, Buy from Chips&Bits: Galactic Civilizations | Galactic Civilizations: Deluxe Edition | Call to Power 2 | Civilization: The Boardgame | GURPS/ Alpha Centauri | Alpha Centauri | Civilization IV | Civilization III: Complete |


Front Page | Civilization IV | Civilization III | Civilization II | Call to Power II | Alpha Centauri | Master of Orion III
Rise of Nations | Galactic Civilizations | Galactic Civilizations II | Misc
Alt.Civs | Civ I | C:CtP I | About | News | Directory | Apolyton Store | Forums | Chat | Columns | Interviews | Newsletter
Scenario League | CSC | Clash of Civs | Spanish Site | CtP Maps | Cradle of Civ | WesW's Ctp1/2 Site | Civ3 Haven

apolyton.net | apolyton.com | civilization2.net | civilization3.net | civilization4.net | civilizationiv.info | calltopower.net | galciv.net | galciv2.net | moo3.net