Regular Expressions + POSIX Dumping Grounds

Discuss programming topics for any language, any source base. If it is programming related but doesn't fit in one of the below categories, it goes here.
Post Reply
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Regular Expressions + POSIX Dumping Grounds

Post by Baker »

A few weeks ago, with the assistance of Lardarse, I managed to get regular expressions working. And after some more experience with it, I thought "This is so easy. I'll never forget".

1 month later ...
me.self wrote:Ok ... aw, sh*t! It isn't working. What was it. How many brackets?
After burning 45 minutes I'll never get back re-remembering, I'm posting this here.

Perhaps someone else might benefit from this.

Regular Expressions in TextPad5

This is from the context of TextPad5. It's what I've used instead of NotePad for a long time. There are other fancy editors out there but so far all the other ones "do too much" for my liking.

It might be exactly the same as in Linux and in Perl or other things or might not be. My understanding is that RegExp and POSIX are a standard so you'd think this would transfer to just about any other Regular Expression queries.

Image

In TextPad, I click Search -> Find in Files and I get greeted with this box.

I want to find EVERY instance of an OpenGL function being used in the original GLQuake source.

OpenGL functions all begin with "gl", but some of the functions in the engine begin with "GL" and a lot of cvars like gl_clear are lower case.
glTexture2d - OpenGL function (wanted)
GL_Init - engine initialization stuff, not wanted
gl_clear - engine console variable, not wanted
So I want to find a lower-case gl followed by an upper case letter and then followed by a lower case letter.

So my regular expression is:
gl [[:upper:]] [[:lower:]] - except there are no spaces ..
Did it work? ...

Image

Yes, except I get some unwanted stuff like glRect -- that isn't a gl function, but of my matches, almost all of them are what I want.

Now, lets say I want to rename all the functions, maybe to reroute all the OpenGL interaction elsewhere in my code ... maybe to capture it or generate some statistics.

I could create duplicate functions like instead of glTexture2d XglTexture2d.

How would I rename those?

First, right click on the search results in TextPad and click "Open All". It opens all the files that search result matches.

Image

Then I do Search -> Find and select "All Documents".

My find expression is the same. Except I need to flag the match part of the expression.
Find text:

gl [[:upper:]] [[:lower:]] -- "gl" followed by 1 uppercase letter and then 1 lower case

New Find Text, we need to put \( \) around the match expression to preserve it in the replacement.

New Find Text for replacement purposes:

gl \( [[:upper:]][[:lower:]] \)

And the Replacement expression:

xgl\1 (the \1 preserves the regular expression part)
Image

And here goes. Did it work?

Image

All clear.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Post by Baker »

Find: (\<[g][l][A-Z][a-z])
Replace: e\1

Finds all gl(capital)(lower) combinations and replaces with the prefix e.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

Post by frag.machine »

I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
Post Reply