#I am so lost...

1 messages · Page 1 of 1 (latest)

agile sparrow
#

Ok so this isnt' working

#

I'm making variables in minecraft functions

#

and the idea is that variables will be used with $(variable name)

mossy olive
#

please use a better thread name next itme

agile sparrow
#

yeah sorry

#

the problem is I don't really know what's not working

#

the .replaceAll("string to replace", "what to replace it with")

#

doesn't work

#

that's my code

mossy olive
#

use regex

agile sparrow
#

?

mossy olive
#
Pattern variablePattern = Pattern.compile("\$([A-Za-z]+)");
Matcher matcher = variablePattern.matcher(text);

StringBuffer result = new StringBuffer();

while (matcher.find()) {
    matcher.appendReplacement(result, variableMap.get(matcher.group(1)));
}
matcher.appendTail(result);
agile sparrow
#

what's the problem with .replaceAll

mossy olive
#

its too much work for a simple problem

#

well an easier problem

agile sparrow
#
tellraw {"text":"$(testVar)"}
#

that's an example of the command at use

#

it identifies that the argument that has "$("

#

then it finds the other parentheses

#

it them saves that portion of text in variable textToReplace and replaces it with variableName, which is the value of the variable

#

why would I use regex for something this simple?

#

either way, it's not replacing text

#

and I have no idea why

#

so that's my main question

mossy olive
agile sparrow
#

what's the problem with my solution though

mossy olive
#

bruh

agile sparrow
#

you just handed me a block of code with absolutely no explanation

#

not knowing what the code does, I would rather stick to the stuff I know

mossy olive
#
// Variable pattern, example: "Hello, my name is $name!"
Pattern variablePattern = Pattern.compile("\\$([A-Za-z]+)");

// Look for matches in the string.
Matcher matcher = variablePattern.matcher(text);

// A mutable string, that the matcher will use for the "substring", or replacement operations.
StringBuffer result = new StringBuffer();

// For match in matches...
while (matcher.find()) {
    // Append the variable data of the match.
    // In other words, the matcher has been adding all of the characters up until this match.
    // Now, we can make our own replacement.
    // In this case, it would be getting the variable stored in our map.
    matcher.appendReplacement(result, variableMap.get(matcher.group(1)));
}

// Append the rest of the string after the last match.
matcher.appendTail(result);
agile sparrow
#

what does mutable mean

mossy olive
#

can be changed

agile sparrow
#

ok

#

so just the oposite of final

mossy olive
#

basically

agile sparrow
#

and what is while

#

just a while loop

#

so what is matcher.find()

mossy olive
#

find the next match

agile sparrow
#

so if you had tellraw {"text":"$(varName)"}

mossy olive
#

you would have to change the regex, but it would still work

agile sparrow
#

it would find $(variableName)

mossy olive
#
\\$\\(([A-Za-z]+)\\)
agile sparrow
#

instead of "\\$([A-Za-z]+)"

agile sparrow
#

wait ok but first what is wrong with my existing program?

#

even if I don't end up using it, I still want to know

mossy olive
#

idk

agile sparrow
#

really?

#

damn it

mossy olive
#
\$\(([A-Za-z]+)\)

regex for $(varName)

"\\$\\(([A-Za-z]+)\\)"

java string (it breaks if it doesnt have extra '\' characters

agile sparrow
#

ok

#

so what is matcher.find()

mossy olive
#

find the next match

agile sparrow
#

ok?

#

still a little confused

mossy olive
#
Pattern pattern = Pattern.compile("a");
Matcher matcher = pattern.matcher("abcdeaf");

while (matcher.find()) {
    System.out.println(matcher.group());
}
agile sparrow
#

I still don't understand 🤷‍♂️

mossy olive
#

Ok

agile sparrow
#

Pattern.compile?

mossy olive
#

compile a regular expression

agile sparrow
#

ok you see I have no idea what that even means

mossy olive
#

it turns this

"\\$\\(([A-Za-z]+)\\)"
#

into readable instructions

agile sparrow
#

ok

mossy olive
#
match $
match (
capture group 1 {
    match (A-Z + a-z) | as many as possible
}
match )
agile sparrow
#

and where is it stored

#

like, how do I replace it, and dispatch that

mossy olive
#

i dont understand