1

需要在组中解析以逗号分隔的算法。

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):

我试图用逗号分隔它们,但没有得到预期的结果:

^Encryption Algorithms:(.*?)(?:,|$)

预期结果是第 1 组中的每个算法没有空组

aes128-ctr
aes192-ctr
aes256-ctr
aes128-cbc
3des-cbc
aes192-cbc
aes256-cbc
4

2 回答 2

1

这可能不是最好的方法,但它可能是将我们的字符串分成三部分的一种方法,甚至可能在通过 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]+)

演示 1

演示 2

正则表达式

如果不需要此表达式,可以在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);

于 2019-05-25T18:49:15.717 回答
0

另一种方法可能是匹配以 开头的字符串Encryption Algorithms:,然后在一个组中捕获一个重复模式,该模式与连字符匹配的部分并在前面加上逗号进行重复。

如果匹配,您可以用逗号分割第一个捕获组。

^Encryption Algorithms:(\w+-\w+(?:,\w+-\w+)*)

解释

  • ^
  • Encryption Algorithms:
  • (开始抓包
    • \w+-\w+匹配 1+ 个单词字符-和 1+ 个单词字符
    • (?:,\w+-\w+)*0+ 次重复逗号,后跟 1+ 个单词字符-和 1+ 个单词字符
  • )关闭捕获组

正则表达式演示| Python 演示

import re
regex = r"^Encryption Algorithms:(\w+-\w+(?:,\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):")

matches = re.search(regex, test_str, re.MULTILINE)
if matches:
    print(matches.group(1).split(","))

结果:

['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', '3des-cbc', 'aes192-cbc', 'aes256-cbc']
于 2019-05-26T15:06:28.560 回答