Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Metacharacters...

Aran

Member

I previously had help from Linkin T on this subject, but I need more info...

So any knowledge must be shared (well, I hope you'll share...)
 

Aran

Member

The Cry of Fallen Angels said:
Try the following;

Type out your exact question(s).
Yes, I suppose you are right...

Well my questions are what are some of the characters and what do they mean? How are they used?

actually that's pretty much it.
 

Aran

Member

but where and how are they used?

EDIT:
Here's an example:

Code:
def make_clock
      clock = $ats.clock
      clock.sub(/\:.*\d/,sprintf(":%02d", $ats.min_is))

wut the hell does that mean??!!?
all i know is
sub(pattern)
but then again, what the hell is a pattern in this sense.

Explain this to me like I'm a 4-year-old...
 
Metacharacters are used inside Regular Expressions to match for groups of characters inside strings. It can look for letters, numbers, symbols, or even words and sentences inside a string. It´s like a search feature for a string and is one of the things that makes Ruby a powerful and versatile language imo. In other words, Regular Expressions says to the interpreter WHAT to search inside the strings, and metacharacters says HOW it could do that.

There´re some rules for using Regular Expressions...
  • Some symbols are considered as metacharacters, and any other symbols matches themselves.
  • Any metacharacter symbol that comes after a backslash is considered as a normal symbol and match itself. (/\\/ => '\', got it?)
  • Some letters and numbers that comes after a backslash are considered as metacharacters
  • Any character that is not a metacharacter matches itself.

String class uses regular expressions. Look inside the class explanation on RMXP help to know which methods uses them. An example:
Code:
string = "Hi i´m Linkin_T."
# This method looks for a match inside the string with the pattern given.
# Returns a piece of the string that represents the first match. Else, returns nil.
# No metacharacters here.
name = string[/Aran/]
p name    =>    nil
# No metacharacters here too.
name = string[/LinkinT/]
p name    =>    nil
# Look that i can put a metacharacter inside the pattern in any moment
name = string[/Linkin/wT/]
p name    =>    "Linkin_T"
# Now note that i put my complete name, and it finds. No metacharacters 
# were used now.
name = string[/Linkin_T/]
p name    =>    "Linkin_T"
# Substituting spaces...
# sub is a String method that looks inside the string an substitutes the first
# match with the string given.
name = string.sub(/\s/, "man, ")
p name    =>    "Hi man, i´m Linkin_T"
# No name!
name = string.sub(/Linkin_T/, "")
p name    =>    "Hi i´m."   (xD)
# Changing name...
name = string.sub(/Linkin_T/, "Aran")
p name    =>    "Hi i´m Aran"    (this reminds me about Trickster and SS... xD)
# That´s it...

Methods that uses regular expressions...
String
  • self[regexp]
  • gsub
  • sub
  • scan
  • slice
  • index
  • rindex
  • split

Well, i think that´s all... Try to look for them and learn on how you can use regexps with them.

Here is a list of things about Regular Expressions...
Programming Ruby":29e6rpfe said:
Regular Expression Patterns

regular characters
All characters except ., |, (, ), [, \, ^, {, +, $, *, and ? match themselves. To match one of these characters, precede it with a backslash.

^
Matches the beginning of a line.

$
Matches the end of a line.

\A
Matches the beginning of the string.

\z
Matches the end of the string.

\Z
Matches the end of the string unless the string ends with a ``\n'', in which case it matches just before the ``\n''.

\b, \B
Match word boundaries and nonword boundaries respectively.

[ characters ]
A character class matches any single character between the brackets. The characters |, (, ), [, ^, $, *, and ?, which have special meanings elsewhere in patterns, lose their special significance between brackets. The sequences \ nnn, \x nn, \c x, \C- x, \M- x, and \M-\C- x have the meanings shown in Table 18.2 on page 203. The sequences \d, \D, \s, \S, \w, and \W are abbreviations for groups of characters, as shown in Table 5.1 on page 59. The sequence c1-c2 represents all the characters between c1 and c2, inclusive. Literal ] or - characters must appear immediately after the opening bracket. An uparrow (^) immediately following the opening bracket negates the sense of the match---the pattern matches any character that isn't in the character class.

\d, \s, \w
Are abbreviations for character classes that match digits, whitespace, and word characters, respectively. \D, \S, and \W match characters that are not digits, whitespace, or word characters. These abbreviations are summarized in Table 5.1 on page 59.

. (period)
Appearing outside brackets, matches any character except a newline. (With the /m option, it matches newline, too).

*
Matches zero or more occurrences of re.

+
Matches one or more occurrences of re.

{m,n}
Matches at least ``m'' and at most ``n'' occurrences of re.

?
Matches zero or one occurrence of re. The *, +, and {m,n} modifiers are greedy by default. Append a question mark to make them minimal.

re1 | re2
Matches either re1 or re2. | has a low precedence.

(...)
Parentheses are used to group regular expressions. For example, the pattern /abc+/ matches a string containing an ``a,'' a ``b,'' and one or more ``c''s. /(abc)+/ matches one or more sequences of ``abc''. Parentheses are also used to collect the results of pattern matching. For each opening parenthesis, Ruby stores the result of the partial match between it and the corresponding closing parenthesis as successive groups. Within the same pattern, \1 refers to the match of the first group, \2 the second group, and so on. Outside the pattern, the special variables $1, $2, and so on, serve the same purpose.
It´s difficult at a first sight, but with practice it comes in hand for you in most cases. The key here is:
  1. Study,
  2. Study,
  3. Study more,
  4. * You know what to do... xD
  5. And remember to test some code to see it working, it helps a lot.

In the example there i used sprintf, but it has nothing about metacharacters. It´s another thing....

Aran":29e6rpfe said:
Explain this to me like I'm a 4-year-old...

And i remember this saying... From a movie... xD
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top