Coding

 Reply to this postStart new topic

How To Use Sanny's Regex Search

Deji
post Jan 5 2011, 11:06 PM
Post #1


Coding like a Rockstar!

Group Icon

Posts: 1,468
From: ???
Joined: 28-May 09



How To Use Sanny's Regex Search



A lot of people either don't notice the regex search option Sanny posseses, or don't know how to use it effectively. Using it will make searches slower, but it can allow you to do many things. For this example, I will show how to turn all script ID's in your source file into constants, like so:

SANNY
0929: init_external_script_trigger 4 (SLOT_MACHINE) with_object_model....
0929: init_external_script_trigger 5 (ROULETTE) with_object_model....
0929: init_external_script_trigger 6 (OTB_SCRIPT) with_object_model....
0929: init_external_script_trigger 7 (ARCADE) with_object_model....
0929: init_external_script_trigger 8 (VENDING_MACHINE) with_object_model....
0929: init_external_script_trigger 9 (FOOD_VENDOR) with_object_model....
0929: init_external_script_trigger 10 (GATES_SCRIPT) with_object_model....
0929: init_external_script_trigger 11 (GYMBIKE) with_object_model....
0929: init_external_script_trigger 12 (GYMBENCH) with_object_model....
0929: init_external_script_trigger 13 (GYMTREAD) with_object_model....
0929: init_external_script_trigger 14 (GYMDUMB) with_object_model....
0929: init_external_script_trigger 15 (BASKETB) with_object_model....
0929: init_external_script_trigger 16 (VIDPOK) with_object_model....
0929: init_external_script_trigger 17 (BLACKJ) with_object_model....
0929: init_external_script_trigger 18 (WHEELO) with_object_model....
0929: init_external_script_trigger 21 (POOL_SCRIPT) with_object_model....


SANNY
const
  // (script ID constants go ere!)
end

0929: init_external_script_trigger SCRIPT_SLOT_MACHINE with_object_model....
0929: init_external_script_trigger SCRIPT_ROULETTE with_object_model....
0929: init_external_script_trigger SCRIPT_OTB_SCRIPT with_object_model....
0929: init_external_script_trigger SCRIPT_ARCADE with_object_model....
0929: init_external_script_trigger SCRIPT_VENDING_MACHINE with_object_model....
0929: init_external_script_trigger SCRIPT_FOOD_VENDOR with_object_model....
0929: init_external_script_trigger SCRIPT_GATES_SCRIPT with_object_model....
0929: init_external_script_trigger SCRIPT_GYMBIKE with_object_model....
0929: init_external_script_trigger SCRIPT_GYMBENCH with_object_model....
0929: init_external_script_trigger SCRIPT_GYMTREAD with_object_model....
0929: init_external_script_trigger SCRIPT_GYMDUMB with_object_model....
0929: init_external_script_trigger SCRIPT_BASKETB with_object_model....
0929: init_external_script_trigger SCRIPT_VIDPOK with_object_model....
0929: init_external_script_trigger SCRIPT_BLACKJ with_object_model....
0929: init_external_script_trigger SCRIPT_WHEELO with_object_model....
0929: init_external_script_trigger SCRIPT_POOL_SCRIPT with_object_model....


In this example, we only have 16 things to replace... This whole replacement works with the factor that Sanny already puts the script names there.

Not too hard to do it manually. But imagine if you had the whole main.scm to replace! Without regex Find & Replace, we have 2 options:

  • Go through each opcode in the main.scm and replace manually.
  • Go through each external script use a normal Find and Replace


I perfer the option "Use regex to replace them all at once" better, though.


For a full understanding of regex (Regular Expressions), I'd say you should go and search around for tutorials and such. The exact format of regex can differ in all sorts of programming languages and programs. In Sanny, there are more restrictions than what the tutorials may give you, but the concept is still solid throughout all implementations.


You're probably getting bored of all this reading now (I'm bored of writing now), so I'll give you a little picture to look at:


You should have "Regular expression" checked before searching. In the top box is the regex I used to perform this replacement operation.

CODE
(\d+) \((.+)\)


Gibberish, yes... and now I'm gonna explain every character. Lucky you.

Lets slim it down a bit to (\d+), which finds a decimal number. We need to get rid of these numbers to replace them with our constants, otherwise we'd end up with extra parameters in the opcode line.

Obviously, you could figure the "d" is for decimal if you payed attention to those last sentances. The backslash before it is to prevent it from being thought of as the letter "d". If we were to search using \d\d\d, we would be finding 3 numbers in a row ("123", for example - "1 2 3" would not match, etc.).

I actually have to note that the brackets ( ) were actually useless in this case, but simply put, the number will be stored so we could use it later if we wanted (more on that later!). The + repeats the last "identifier" (in this case, \d) until it is broken. So, \d+ would find a number of any length, such as "8123915029", "8" or "fa021adsfk" ("021" is found).

\d+.\d+ could return a float, such as "512.389".


Next, we have a space, which is matched as normal. After that, we have \((.+)\) - the terrifying bit!

Not so terrifying, really. This time, the backslashes stop the brackets ( ) from being used as part of the regex identifier.

"\(" = "("

This is so we can match the brackets around the script name in (SCRIPT_WHEELO) etc.


The brackets which don't have a backslash before them wrap around a bit that we want to save/keep for later. We can refer to it by $2 (because $1 would contain the decimal we matched earlier). These are not global variables! Just something the regex parser sees and replaces with the value.


The dot . matches any character (except a newline), but only does so once. So we use the + to repeat it. This gives us the script name, which is what will be stored so we can access it in the replace field with $2. We didn't save the brackets, though.. or the spaces, so they will be thrown away.

This works well in the main.scm - because the only time when there's a number followed by text in brackets is when it's a script ID, so we only replace what we want.


In the replace field, we simply have this:
CODE
SCRIPT_$2


This puts a prefix on the script name which was in brackets... The script name replaces $2, because it's the 2nd thing we wrapped in ( ) (remember: backslashes don't count!) - Now we just click "Replace" and they all change nice and quick.



Any questions, fire away... I made this out of boredom and ended this the same way. Cya.


--------------------
Go to the top of the page
 
+Quote Post
Reply to this postStart new topic

1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: