I’ve seen some similar questions, but was wondering about a more specific case. I wrote a Stack Exchange answer, and would like to use it in one section of a book I’m writing (which will be commercially sold). I also modified the answer, and added to it, but some original passages remain. I’m wondering if, as the author, and with proper attribution that the section is based on my Stack Exchange answer, with a link, this is legally acceptable?
Yes, that’s allowed. Under the Stack Exchange terms of service, content you upload is licensed to Stack Exchange Inc. on a non-exclusive basis under CC-BY-SA 4.0. The terms of service do not give Stack Exchange the copyright to your contributions, and a non-exclusive license means you are not promising Stack Exchange that “only Stack Exchange will be allowed to use this content.” That means you can continue to do whatever you want with your own content and do not need to mention Stack Exchange at all. The only restriction is that you can’t stop Stack Exchange from continuing to use your Stack Exchange content under CC-BY-SA 4.0, and since it’s a Creative Commons license you also can’t stop anyone else from using your Stack Exchange content under that license.
I also modified the answer, and added to it, but some original passages remain.
If the content is strictly your own plain text and equations that's one thing, but some posts may also contain block quotes from other sources and/or images, and their status will have to be examined separately and individually beyond SE's CC-BY-SA.
I believe there is another issue you may have to consider, and that's your contract with the book publisher. You have already granted some rights to Stack Exchange, and that means you cannot assign all rights to the publisher, which they may by default want you to do.
The thing is, this is between you and the publisher and they will probably be okay if they don't have exclusive rights to a few paragraphs of the book. I am just making the point that the publisher also does have a stake in what you have done with the material before.
I am very much not a lawyer but this might be something to ask more knowledgeable people about.