+ Reply to Thread
Results 1 to 6 of 6

Thread: How to use findRE and replaceRE

  1. #1
    Wirererer OmicroN will become famous soon enough OmicroN's Avatar
    Join Date
    Apr 2008
    Location
    Frozen Hell (aka Michigan)
    Posts
    144

    Default How to use findRE and replaceRE

    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.

  2. #2
    Wire Sofaking N00bDud3 has a spectacular aura about N00bDud3 has a spectacular aura about N00bDud3's Avatar
    Join Date
    Jul 2009
    Location
    Error: Unknown Location
    Posts
    971

    Default Re: How to use findRE and replaceRE

    This is a very useful function. Now it is much easier to find/replace specific parts of strings. This may be useful in my OS I am working on.


  3. #3
    <title>eh?</title>


    Drunkie is a jewel in the rough Drunkie is a jewel in the rough Drunkie is a jewel in the rough Drunkie's Avatar
    Join Date
    Feb 2009
    Location
    Canada
    Posts
    2,411

    Default Re: How to use findRE and replaceRE

    Nice tutorial! I was wondering what those functions did awhile ago.
    Tutorials: Wire GPU | 3D Projections

    Black Phoenix: drun KIE
    Black Phoenix: PLEASE CONFIRM THIS MESSAGE DRUN KIE
    Drunkie: confirmed
    Black Phoenix: THE INTERGALACTIC CONTINIUM WANTS DRUN KIE TO GO ON A SPECIAL MISSION
    Black Phoenix: this will be AN EASY mission TO THE UNKNOWN AND beyonD
    <HP> ... I can't believe I'm talking to a fucking script at 4 am

  4. #4
    Lurker robertark is an unknown quantity at this point robertark's Avatar
    Join Date
    Jun 2007
    Location
    Northwest Arkansas
    Posts
    51

    Default Re: How to use findRE and replaceRE

    How come this regex function doesn't include delimiters like normal languages do? Do they include flags like g (Global), i (Ignore case) or m (Multi-line)?

    Just wondering, it'd be awesome to have a regex function like if you wanted to match mm/dd/yyyy:

    ValidDate = String:findRE("/(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}/")

    Would be cool, but possibly not necessary for gmod heh
    "Knowledge is power, and without power you're nobody." -Self



  5. #5
    Wirererer sk89q has a spectacular aura about sk89q has a spectacular aura about sk89q has a spectacular aura about sk89q's Avatar
    Join Date
    Sep 2009
    Posts
    254

    Default Re: How to use findRE and replaceRE

    Lua doesn't use Perl/POSIX regular expressions. It uses its own simple pattern matching language to make Lua small.
    Tabbed Prop Spawn Menu
    My Website, KAF's Anime Music



    Build: F6 quick save, PewPew, interactive tutorial system, custom tools
    Wire: UWSVN, High Speed Snapshot Viewer, EGPv3, bunch of Wiremod fixes
    E2: Prop Core, Constraint Core, Entity Core, holoModelAny(), applyPlayerForce(), etc.
    We have modified and rewritten a number of our addons.

  6. #6
    Wire Sofaking Dav1d will become famous soon enough Dav1d's Avatar
    Join Date
    May 2009
    Posts
    697

    Default Re: How to use findRE and replaceRE


    3chars
    Last edited by Dav1d; 02-09-2010 at 08:58 AM. Reason: It's now 3 chars what?
    Quote Originally Posted by initrd.gz View Post
    Don't quote me though.

+ Reply to Thread

LinkBacks (?)

  1. 01-28-2010, 11:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots