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 _
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.