Bug - Fixed Escaping numbers in strings


When attempting to make use of strings that included escaped digits (eg "\1"), an unexpected error / debug log is triggered. While I initially ran into this while trying to make use of regexp backreferences, it appears to happen in other uses as well.

My memory is somewhat rusty on the subject, so if I'm simply doing it wrong please let me know. ;)

System Info:
KoLmafia version: r10015
OS: Windows XP
Jave: v1.6.0_11-b03


  • DEBUG_20111120.txt
    6.7 KB · Views: 50


A backslash followed by a digit is the start of an octal character escape, which is one way of inserting un-typeable characters in a string. The debug log is unfortunate, but this IS an actual error in your code - three digits are required in this case.

To include "\1" in your regexp, you have to double the backslash so that an actual backslash ends up in your string.


Just to make sure I have this correct, I should be using:
matcher m = create_matcher(".+?(\\d+)",some_string);
print("Found the digits in there of: \\1");

For example, if I want to append the first group to my string? For some reason this sounds as if it's different than what I was told before, and indeed does not appear to work as I'm expecting. For reference, I was once told after posting some code that made use of "matcher.group(int)" style that I should just type "\int" instead... I dunno, like I said, I'm confused...


\1 works to refer to a group only within the same regex that defined that group. In replace_all(), replace_first(), and append_replacement() you can use $1 in the replacement string to refer to a group - I'm not sure why Java uses $ here, most languages use a backslash in both the replacement and the pattern. Elsewhere in your script, m.group(1) is your only way of retrieving the group.


Ah, ok, that would be the bit I'm mis-remembering. Thanks for clarifying.

The debug log for escaped numbers is still weird though. ;)


Active member
That debug log simply says that the \1"); part of your print("....\1"); source code is invalid as an escape sequence.

And the $ thing is from perl. I think that entire package is explicitly designed to be as close to perl syntax as possible.
Of course - in perl, that $1 is a global (or possibly local-ed) variable that captured the can be used to produce the captured value forever (until the next matching). So you *could* do there a
some_string =~ /.+?(\\d+)/;
print "Found the digits in there of: $1";