7

This question was earlier titled as just 'Ill-formatted code error preventing answer submission'. After finding the cause, it was amended to help potential future searches. See my answer that follows.

I am trying to post an answer on SO but it keeps showing ill-formatted code error. It renders fine in the preveiw though. What's the issue?

Error msg:

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon

Answer Text:

As per Java package naming conventions, the basic syntax

  • allows only lower case English alphabets,numbers, _ and .
  • must start with alphabet
  • can't be malformed eg: contains .. | endsWith . | contains Java keywords

Ignoring the keyword constraint, the regex can be

^[a-z][a-z0-9_]*(\.[a-z0-9_]+)*[a-z0-9_]*$

//regex breakdown
//^[a-z]            start with one lowercase English alphabet  
//[a-z0-9_]*        followed by zero or more of alphabets, numbers or _  
//(\.[a-z0-9_]+)*   zero or more of these  
//                          .(one or more of alphabet|number|_)  
//[a-z0-9_]*$       must end in alphabet|number|or _

try

This differs from @gvalkov's answer by

  • allowing the number of word.word constructs to be zero
  • augmenting last char check with * to correctly detect last char even in presence of word.word matches

FAQs

What exactly is it that you are entering in the answer box?

The text I actually enter can be seen by viewing this question in Edit mode. The actual text starts at the comment <!-- and ends with the url that ends like /r/wpryj1/3. These FAQs are posted below that.

Isn't your question a duplicate of this one?

No. Not only does my post not have a title - the apparent cause of error in the linked question - the suggested solution, a blank line preceding the code block doesn't apply (as I already have a blank line preceding my code block) nor does it work.

Have you tried other variations? Switch browser? Use ````` instead?

Yes, I have. Same problem exists on Chrome as well (I was posting on Firefox). I did try using the back-tick based formatting as that is how I usually type code on SO but that didn't work either and the error message suggested to put 4 spaces at the start.

Did you try removing the | symbols?

Yep. Doesn't work

Were you able to isolate the fault to any particular section?

Yes, I was. Its definitely in the code block. If I delete it entirely, the error goes away.

Does the text post here, as an answer?

Yes.

Were you able to post your answer some other way? Maybe not use code based formatting?

Luckily, once I rewrote every single character of the code in the answer, the answer posted fine. What puzzles me is as given there isn't any invisible character present that's messing with the formatting. Nor have I faced this error earlier over countless code copy-pastes.

Why did you to bother putting the question up on meta then?

Errors which prevent potential answer writers from submitting their answers are bad for SO or SE in general. Especially, when the cause of the error is not obvious and the error message unhelpful. Seeing that there are many other similar questions about puzzling errors stopping submissions, this is concerning. Such errors are incredibly frustrating and consume unnecessary effort and time. I was lucky that something worked and I was able to post, but frankly, I most likely would not have answered (or added an answer) at all if it wasn't for the time I had on my hands.

9
  • 5
  • @JeanneDark nope. no title. adequate spaces and lines before code block. different issue, it seems. just tried
    – lineage
    Commented Sep 13, 2021 at 20:15
  • 1
    Maybe it’s getting thrown by the pipes in the 3rd bullet point?
    – BSMP
    Commented Sep 13, 2021 at 22:07
  • Have you tried using triple backticks instead of four spaces?
    – BSMP
    Commented Sep 13, 2021 at 22:21
  • *Java. *English. Commented Sep 13, 2021 at 23:23
  • @BSMP nope. tried without | ...doesn't post
    – lineage
    Commented Sep 13, 2021 at 23:54
  • @BSMP ``` is how I usually write...switched to 4 spaces seeing the error msg
    – lineage
    Commented Sep 13, 2021 at 23:55
  • Does changing your browser help? Will it let your post this as an answer here, without any of the question text?
    – BSMP
    Commented Sep 14, 2021 at 1:25
  • @BSMP no same issue on other browsers (Firefox & chrome). yes does post on SOmeta
    – lineage
    Commented Sep 14, 2021 at 2:59

1 Answer 1

1

My fault isolation was wrong. In my case the error only disappears when I change the top comment from

<!--
https://stackoverflow.com/q/29783092/10115137
-->

to

<!--https://stackoverflow.com/q/29783092/10115137-->

So the wrongly formatted comment was the issue. Though it wasn't in code, the error message biased me towards trying to find the one phantom code formatting typo that must be breaking everything. There was none. The dubious error message made things worse.

I went back and re-checked my post. In the version that I eventually did get working and ultimately posted, I had inadvertently deleted the comment - that's what falsified the fault isolation and deduced cause.

Do note that the same correction isn't needed when posting on SOMeta, PhysicsSE, MathematicsSE/Meta and MathematicaSE. The multiline comment breaks, so far, only on SO.

Ironically, the cause proffered in the upvoted suggested link (to an unrelated question) was of no trouble at all - my answer posts fine with the lone correction above, blank line above code or not. Even further, the suggested original has now become a non-issue as the example code provided there posts fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .