#I am so lost...
1 messages · Page 1 of 1 (latest)
I'm making variables in minecraft functions
and the idea is that variables will be used with $(variable name)
please use a better thread name next itme
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
use regex
?
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);
what's the problem with .replaceAll
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
my solution is so much easier
what's the problem with my solution though
bruh
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
// 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);
what does mutable mean
can be changed
basically
find the next match
so if you had tellraw {"text":"$(varName)"}
you would have to change the regex, but it would still work
it would find $(variableName)
how so
\\$\\(([A-Za-z]+)\\)
instead of "\\$([A-Za-z]+)"
continuing, it would find that? and save that as a string I think?
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
idk
\$\(([A-Za-z]+)\)
regex for $(varName)
"\\$\\(([A-Za-z]+)\\)"
java string (it breaks if it doesnt have extra '\' characters
find the next match
Pattern pattern = Pattern.compile("a");
Matcher matcher = pattern.matcher("abcdeaf");
while (matcher.find()) {
System.out.println(matcher.group());
}
I still don't understand 🤷♂️
Ok
Pattern.compile?
compile a regular expression
ok you see I have no idea what that even means
ok
match $
match (
capture group 1 {
match (A-Z + a-z) | as many as possible
}
match )
i dont understand