1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Irregular Expressions

Discussion in 'Snippets' started by Tenfont, Jan 22, 2022.

  1. Tenfont

    Tenfont Member

    Mar 28, 2021
    Likes Received:
    Irregular Expressions
    by TenFont

    Irregular Expressions are code snippets that I created by utilizing RegEx (Regular expressions). Credit is given where credit is due.

    1 Floor Timespan
    A simple function to round down a timespan, eliminating all decimals.
    Code (Skript):
    1. function floorTimespan(time: timespan) :: timespan:
    2.     set {_s} to join (regex split "%{_time}%" at ".\d+")
    3.     return {_s} parsed as timespan

    Code (Skript):
    1. set {_a} to "2 minutes and 37.49 seconds" parsed as timespan
    2. broadcast floorTimespan({_a})
    3. # output: 2 minutes and 37 seconds

    2 Split String At Every N Characters
    Efficiently splits a string at every N characters.
    Code (Skript):
    1. function splitAt(str: string, n: integer) :: strings:
    2.     if {_n} > 0:
    3.         return regex split {_str} at "(?<=\G.{%{_n}%})"
    4.     return {_str}
    Code (Skript):
    1. broadcast splitAt("ABCDEFG", 2)
    2. # output: AB, CD, EF and G

    3 Format A Number With Commas
    Format a number with commas, making it more readable. Also accepts decimals.
    Code (Skript):
    1. function formatNumber(x: num) :: string:
    2.     set {_x::*} to split "%{_x}%" at "."
    3.     set {_x::1} to (join (regex split {_x::1} at "(?<=\d)(?=(\d\d\d)+(?!\d))") by ",")
    4.     return join {_x::*} by "."
    Code (Skript):
    1. broadcast formatNumber(10000000.389)
    2. # output: 10,000,000.39

    You can find all of my updated snippets at:
    #1 Tenfont, Jan 22, 2022
    Last edited: Feb 23, 2022

Share This Page