A lot of people do not know of or use these functions, and I am here to help after being helped by BenBob on a good way to use it.
I've recently updated the string section of the E2 Wiki to include the replaceRE() function, this is it explained.
First off, the RE is short for Regular Expression. Those of you who are familiar with programs like excel and access may know what this is already. This type is also used in programing languages like Java.
When it comes to expressions within the functions, they are held with brackets [ ], every different letter/number is held as a separate command unless specified otherwise.
Important keys to remember are, ^ and -
^ can be thought of to be a != function. To say [^1] it would return everything but 1.
Let us use a test sentence to manipulate to test our functions.
Code:
Test = "abcdefg ABCDEFG 1234567 Hello my name is Bob"
Now, let's say we want to remove all of the numbers in the string. You can place the numbers you want to remove by using [1234] or you can do ranges like [1-4].
Code:
Test:replaceRE("[0-9]","") returns "abcdefg ABCDEFG Hello my name is Bob"
▲ ALTERNATIVE ▼
Test:replace("1",""):replace("2",""):replace("3",""):replace("4",""):replace("5",""):replace("6",""):replace("7","")
Keep in mind this will not remove the spaces inbetween
If we wanted to remove all instances of lowercase letters, our code would be.
Code:
Test:replaceRE("[a-z]","") returns " ABCDEFG 1234567 H B"
▲ ALTERNATIVE ▼
Test:replace("a",""):replace("b",""):replace("c",""):replace("d",""):replace("e",""):replace("f",""):replace("g","")
If we wanted to remove all instances of UPPERCASE letters, our code would be.
Code:
Test:replaceRE("[A-Z]","") returns "abcdefg 1234567 ello my name is ob"
▲ ALTERNATIVE ▼
Test:replace("A",""):replace("B",""):replace("C",""):replace("D",""):replace("E",""):replace("F",""):replace("G","")
Finally if we wanted to remove all instances of letters, our code would be.
Code:
Test:replaceRE("[a-zA-Z]","") returns " 1234567 "
or
Test:replaceRE("[A-z]","") returns the same.
▲ ALTERNATIVE ▼
Test:lower():replace("a",""):replace("b",""):replace("c",""):replace("d",""):replace("e",""):replace("f",""):replace("g","")
The alphabet is read like ABC...Zabc...z so [A-z] covers the entire area.
Let's say we wanted to remove all instances of lowercase vowels, our code would be. *added Y cause spaghetti-o's told me to*
Code:
Test:replaceRE("[aeiouy]","") returns "bcdfg ABCDEFG 1234567 Hll m nm s Bb"
▲ ALTERNATIVE ▼
Test:replace("a",""):replace("e",""):replace("i",""):replace("o",""):replace("u",""):replace("y","")
Now that we've gotten that down, lets start considering other options. Lets say we really don't want those annoying spaces in the code, we just simply have to add a " " somewhere within the bracket
Code:
Test:replaceRE("[aeiouy ]","") returns "bcdfgABCDEFG1234567HllmnmsBb"
▲ ALTERNATIVE ▼
Test:replace("a",""):replace("e",""):replace("i",""):replace("o",""):replace("u",""):replace("y",""):replace(" ","")
As I explained earlier there are functions within the expressions that you can use like ^. We've covered - as being used for ranges such as A-E and F-Z
to remove everything but lowercase letters we'd use this
Code:
Test:replaceRE("[^a-z]","") returns "abcdefgellomynameisob"
Now to remove everything but UPPERCASE letters we'd use this
Code:
Test:replaceRE("[^A-Z]","") returns "ABCDEFGHB"
------------------------- ENDS replaceRE() tutorial -------------------------
Now we're on to findRE() and what that's about...
findRE(S,N) - findRE(S) are functions that use a "Regular Expression" to do a lookup in a string. We're going to use the same string that I used previously.
Code:
Test = "abcdefg ABCDEFG 1234567 Hello my name is Bob"
I mainly use this function to determine if a word or a letter is within a sentence.
For example let's say I want to find the out where in a string the first CAPITAL letter is at
Code:
Test:findRE("[A-Z]") returns 9
the reason it returns 9 is that the first capital is located at index # 9
A good use for this is to use it with functions like S:left(N), S:right(N), and I guess S:index(N)
Code:
Test:right(Test:length()-Test:findRE("[A-Z]")) returns "BCDEFG 1234567 Hello my name is Bob"
Test:left(Test:findRE("[A-Z]")) returns "abcdefg A"
Test:right(Test:length()-Test:findRE("[0-9]")) returns "234567 Hello my name is Bob"
Test:left(Test:findRE("[0-9]")) returns "abcdefg ABCDEFG 1"
Test:index(Test:findRE("[0-9]")) returns "1"
I've been using terms like 0-9, a-z, and A-Z in the RE's, but you can use so much more. To use more than one range or conditional you can simply using terms like "[A-CE-G]", which would be finding capitals between A and C, also between E and G.
You can also change ranges like 1-5 or 5-9, also a-b and E-T.
Bookmarks