SecondThread's blog

By SecondThread, history, 17 months ago, In English

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 :)

  • Vote: I like it
  • +112
  • Vote: I do not like it

»
17 months ago, # |
  Vote: I like it +23 Vote: I do not like it

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.

»
17 months ago, # |
  Vote: I like it +31 Vote: I do not like it

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) {
    }
}
  • »
    »
    17 months ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    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

»
17 months ago, # |
  Vote: I like it -42 Vote: I do not like it

Who needs Java, lol?