Irregular Expressions

  • 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!

Tenfont

Staff member
Moderator
Mar 28, 2021
32
3
8
19
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_language.skript:
function floorTimespan(time: timespan) :: timespan:
    set {_s} to join (regex split "%{_time}%" at ".\d+")
    return {_s} parsed as timespan
code_language.skript:
set {_a} to "2 minutes and 37.49 seconds" parsed as timespan
broadcast floorTimespan({_a})
# output: 2 minutes and 37 seconds


2 Split String At Every N Characters
Efficiently splits a string at every N characters.
code_language.skript:
function splitAt(str: string, n: integer) :: strings:
    if {_n} > 0:
        return regex split {_str} at "(?<=\G.{%{_n}%})"
    return {_str}
code_language.skript:
broadcast splitAt("ABCDEFG", 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_language.skript:
function formatNumber(x: num) :: string:
    set {_x::*} to split "%{_x}%" at "."
    set {_x::1} to (join (regex split {_x::1} at "(?<=\d)(?=(\d\d\d)+(?!\d))") by ",")
    return join {_x::*} by "."
code_language.skript:
broadcast formatNumber(10000000.389)
# output: 10,000,000.39

You can find all of my updated snippets at:
https://tenfont.gitbook.io/snippets/
 
Last edited: