Блог пользователя SecondThread

Автор SecondThread, история, 2 года назад, По-английски

Codeforces appears to have a minor bug in the backend regex evaluation for determining which submissions are legal java code. When attempting to submit, if you have an open curly brace in a submission before your outer class, you receive the following error:

Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*

This happens even if the curly brace is in a comment. Here's an example case it won't let you submit when really it should:

Break case

Here's the thing though: according to regex101 the regex is correct, and the above java code does indeed match (with PHP >=7.3 regex, the default). This makes me believe there's some bug in how CF is evaluating whether the regex matches, rather than with the regex itself.

Why it matters

Although it's not a super big deal, often times I will use a comment block above my main class as a note pad to think about the problem, and whenever I use curly braces in my notes, if I don't go back and delete them before submitting, CF doesn't accept the submission.

Alright, that's all. Happy debugging, and let me know if I can help :)

  • Проголосовать: нравится
  • +112
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится +23 Проголосовать: не нравится

The regex [^{}]*public\s+(final)?\s*class\s+(\w+).* does indeed not match the full source code mentioned, it only matches a substring (that's why on regex101 it's accepted). If Codeforces tries to match the full source code, which is what Pattern.matches does in Java (the language the check was likely written in), then it evaluates the regex correctly in this case.

It also seems like Codeforces uses this regex to "parse" your code to determine the correct filename to use, etc., which can cause even more problems (using regex to decide a non-regular language isn't a good idea). I think the check should be removed or done in a different way.

»
2 года назад, # |
  Проголосовать: нравится +31 Проголосовать: не нравится

This regex has to match the entire source code, not just some substring of it. In python terms, codeforces uses re.fullmatch whereas the regex site you linked uses re.search. This particular regex simply doesn't handle comments at all.

It's worth noting that you can annul the entire regex check with a comment that looks like a class declaration:

// public class Huzzah! {

/*
 }
*/
public class A {
    public static void main(String[] args) {
    }
}
  • »
    »
    2 года назад, # ^ |
      Проголосовать: нравится +13 Проголосовать: не нравится

    Lol you're right, that's hilarious. If the regex isn't being used to find the name of the class, it sounds like the check should indeed just be removed entirely

»
2 года назад, # |
  Проголосовать: нравится -42 Проголосовать: не нравится

Who needs Java, lol?