这可能不是最好的方法,但它可能是将我们的字符串分成三部分的一种方法,甚至可能在通过 RegEx 引擎运行它之前。如果情况并非如此,并且我们希望有一个表达式,那么这可能很接近:
(.+Encryption Algorithms:)|([a-z0-9-]+)(?:,|\s)|(MAC.+)

如果您也有新行,您可能希望使用其他表达式进行测试,可能类似于:
([\s\S]+Encryption Algorithms:)|([a-z0-9-]+)(?:,|\s)|(MAC[\s\S]+)
([\w\W]+Encryption Algorithms:)|([a-z0-9-]+)(?:,|\s)|(MAC[\w\W]+)
([\d\D]+Encryption Algorithms:)|([a-z0-9-]+)(?:,|\s)|(MAC[\d\D]+)
正则表达式
如果不需要此表达式,可以在regex101.com中对其进行修改或更改。
正则表达式电路
jex.im可视化正则表达式:

测试
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"([\w\W]+Encryption Algorithms:)|([a-z0-9-]+)(?:,|\s)|(MAC[[\w\W]+)"
test_str = ("SSH Enabled - version 2.0\n"
"Authentication methods:publickey,keyboard-interactive,password\n"
"Encryption Algorithms:aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc\n"
"MAC Algorithms:hmac-sha1,hmac-sha1-96\n"
"Authentication timeout: 120 secs; Authentication retries: 3\n"
"Minimum expected Diffie Hellman key size : 1024 bits\n"
"IOS Keys in SECSH format(ssh-rsa, base64 encoded):\n")
subst = "\\2 "
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
演示
const regex = /(.+Encryption Algorithms:)|([a-z0-9-]+)(?:,|\s)|(MAC.+)/gm;
const str = `SSH Enabled - version 2.0 Authentication methods:publickey,keyboard-interactive,password Encryption Algorithms:aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc MAC Algorithms:hmac-sha1,hmac-sha1-96 Authentication timeout: 120 secs; Authentication retries: 3 Minimum expected Diffie Hellman key size : 1024 bits IOS Keys in SECSH format(ssh-rsa, base64 encoded):`;
const subst = `$2 `;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);