所有非数字字母均去掉:

strs = 'public void testNoXmlHeader() throws IOException { final String xml = "<text>text with no XML header</text>"; checkXmlContent(xml, "UTF-8", null); }'
result = re.sub('\W+', '', strs).replace("_", '')
print(result)
publicvoidtestNoXmlHeaderthrowsIOExceptionfinalStringxmltexttextwithnoXMLheadertextcheckXmlContentxmlUTF8null

保留空格数字和字母:

strs = 'public void testNoXmlHeader() throws IOException { final String xml = "<text>text with no XML header</text>"; checkXmlContent(xml, "UTF-8", null); }'
result = re.sub('[^A-Za-z0-9 ]+', '', strs).replace("_", '')
print(result)
public void testNoXmlHeader throws IOException  final String xml  texttext with no XML headertext checkXmlContentxml UTF8 null