mc.roleplayhub.com

players online

[AdvancedChat] Regular Expressions

Customable

Level 146
Administrator
School Clubs Lead
Media Team
Customable
Customable
Omega+

Hello advancedChat users, this thread discusses the use of RegEx in advancedChat chat tabs.


What is RegEx?

RegEx is short for Regular Expressions, it is a string of symbols which mark filters for certain word placement, most of you who use advanced chats are likely used to the Literal option when filtering certain lines from your chat, literal can be useful when wanting to filter precise words however is not quite as powerful as regex is when it comes to sentence filtering.


How to start?

Lets begin with a website: https://regex101.com

regex101 is the perfect website for testing and beginners, it hosts a documentation of expressions and has a testing box which allows you to build expressions on the fly and see how and if they function.


Lets begin

lets start with a basic expression, Meta Escape ( ^ , $ ), this expression allows us to filter a word at the start or end of a sentence.


as you can see, this will highlight a small purple line at the beginning/end of the sentence. its important to note that these expressions can not be directly mixed together:


A group can be used to mix the expressions but groups will be explained further later later



now you may be wondering, what can I do with a meta escape?

You can now use this beginning expression to filter certain words or amounts of words, here are a few examples:


This will literally filter the word First (cap sensitive) at the beginning of any sentence


This defines a match all characters meta escape ( . ) will group all characters in a character class ( [] ) and defines a range in that class for all characters between a-z, A-Z and 0-9. It then provides a Quantifier ( + ) which will match the previous range unlimited times.

because the character class doesn't have a space in its matches, it will only define the word at the start of the sentence due to the beginner meta escape. This can be eliminated by simply introducing a space into the character classes matches or, eliminating the beginner meta escape





This expression feels very packed, it can be overwhelming to understand, but it can actually be simplified by introducing a character quantifier ( * ), I personally like to call this character a wild card because it will match ANY character that exists, including spaces.



as seen here, this is much easier to understand



you may have noticed by now that certain characters are used to filter certain things, but what if you want to use one of those characters as a filter itself?

well then you can use an Escape character ( \ ), this will allow you to cancel a special character in an expression and use it as a regular character


square brackets are usually used to define character classes, but here you can see it is now acting as a regular character for filtering


I mentioned groups earlier, groups ( () ) are very useful for filtering multiple regex sequences in one line or multiple words/characters


as you can see here, I have used a group to filter just the brackets in this sentence, a similar effect can be achieved with a range, however this is much easier to understand



This example filters just the words hello, hi and bye out of the test string


With everything above mentioned, we now have all the necessary knowledge to completely filter all IC chat messages. surprisingly simple, right?



There are two regex groups here, I will explain them separately:

This expression checks the beginning of the sentence with a Meta Escape ( ^ ), it will then filter an asterisk as a non special character using an escape character ( \ ), it will add an unlimited quantifier so it filters as many asterisks as it needs to at the start of the sentence, after this it then introduces a character match ( . ) and an unlimited quantifier ( * ) at the end to filter any and all characters after this.

this will filter ALL /its and /mes

This expression is much harder to understand, so lets split it into five to make it easier to understand.


^.+

This part filters anything at the beginning of the sentence with a Meta Escape ( ^ ), it then introduces a character match ( . ) and a quantifier ( + ) to match any characters infront of that until the next section.


\[.+]

This part filters the opening square bracket with an escape character ( \ ) then matches any sequence of characters in front of that opening bracket with a character match ( . ) and a quantifier ( + ), it then cuts it off by introducing a closing square bracket.

You may be wondering why the closing bracket didn't need an escape character, it defines a character class right?

the close square bracket only defines a character class if a open bracket is present as a special character, this actually goes the same for groups and precise quantifiers ( {} ) which is not covered in this.


.+

This is a character match ( . ) and a quantifier ( + ) which filters any characters here, no matter the length.


(says|whispers|yells)

This group will now filter any of the words says, whispers and yells if one of them is present.


("|')

this group will filter between quotes and commas as /yell and normal typing tend to alternate between the two, this group covers all our bases similar to says, whispers and yells

this is repeated twice, once at the end of the type of speech group and once more at the end.


.*

Finally, this filters any and all characters, even if there are no characters there at all using a character match ( . ) and a zero to unlimited quantifier ( * )


This will come together to filter all of the following:

Nickname [Role] RPname says ""
Nickname [Role] RPname whispers ''
Nickname [Role] RPname yells ''

Using this line, you can now single handily filter ALL IC chat messages into a chat tab using a Regular Expression.




 

Vexosphere

Level 43
Vexosphere
Vexosphere
Notable
i didn't understand a single thing from it but cool tutorial (i'm just too dumb for this mod)

oh and it's worth to mention that someone updated this mod for 1.21.1! slay
 

Users who are viewing this thread

Top