给定(相对较长)的字符串:
string
= "Checks for load balancers with listeners that do not use recommended security configurations for encrypted communication. AWS recommends using a secure protocol (HTTPS or SSL), up-to-date security policies, and ciphers and protocols that are secure.
\nWhen you use a secure protocol for a front-end connection (client to load balancer), the requests are encrypted between your clients and the load balancer, which is more secure.
\nElastic Load Balancing provides predefined security policies with ciphers and protocols that adhere to AWS security best practices. New versions of predefined policies are released as new configurations become available.
\nAlert Criteria
\nYellow: A load balancer has no listener that uses a secure protocol (HTTPS or SSL).
\nYellow: A load balancer listener uses an outdated predefined SSL security policy.
\nYellow: A load balancer listener uses a cipher or protocol that is not recommended.
\nRed: A load balancer listener uses an insecure cipher or protocol.
\nRecommended Action\n
\nFor more information, see Listener Configurations for Elastic Load Balancing.
\nAdditional Resources
\nListener Configurations Quick Reference
\nUpdate SSL Negotiation Configuration of Your Load Balancer
\nSSL Negotiation Configurations for Elastic Load Balancing
\nSSL Security Policy Table
\n"
我想有一个方法,我将其中一个状态作为参数传递:
'Green'
'Yellow'
'Red'
这将返回一个完整的sencentes数组,跟随这个字符串(无论有多少出现的字符串).
def status_description(string, status) # manipulate string and return status description(s) end
有了上面的字符串,我希望
status_description(string, 'Yellow')
回来
[ 'A load balancer has no listener that uses a secure protocol (HTTPS or SSL).', 'A load balancer listener uses an outdated predefined SSL security policy.', 'A load balancer listener uses a cipher or protocol that is not recommended.' ]
和
status_description(string, 'Red')
回来
['A load balancer listener uses an insecure cipher or protocol.']
字符串将始终具有相同的结构,这意味着状态描述始终遵循以下部分:
\nAlert Criteria
如果你可以让方法返回一个包含所有状态的哈希(通常是前面提到的三个中的一部分或全部),那么它的'描述就完美了!就像是:
{ 'Green' => ['some green desc'] 'Yellow' => ['some yellow desc', 'another yellow desc'], 'Red' => ['some red desc'] }
我还需要得到以下数组'Recommended Action'
:
[ 'If the traffic to your load balancer must be secure, use either the HTTPS or the SSL protocol for the front-end connection.', 'Upgrade your load balancer to the latest version of the predefined SSL security policy.', 'Use only the recommended ciphers and protocols.' ]
我对正则表达式几乎没有经验,在这种情况下可能不是那么微不足道.
非常感谢你的帮助!
def status_description(str, color) str.scan(/(?<=#{color}:\s).*?[.!?]/i) end status_description(string, "yellow") #=> ["A load balancer has no listener that uses a secure protocol (HTTPS or SSL).", # "A load balancer listener uses an outdated predefined SSL security policy.", # "A load balancer listener uses a cipher or protocol that is not recommended."] status_description(string, "green") #=> [] status_description(string, "red") #=> ["A load balancer listener uses an insecure cipher or protocol."]
对于
color = "yellow"
正则表达式是
r = / (?<= # begin a positive lookbehind #{color} # match the value of the variable `color` :\s # match a colon followed by whitespace ) # close positive lookbehind .*? # match any number of any characters, lazily [.!?] # match a character that terminates a sentence /ix # case-indifference and free-spacing regex definition modes #=> / #=> (?<= # begin a positive lookbehind # yellow # match the value of the variable `color` # :\s # match a colon followed by whitespace # ) # close positive lookbehind # .*? # match any number of any characters, lazily # [.!?] # match a character that terminates a sentence # /ix
或者,根据要求,
["green", "yellow", "red"].each_with_object({}) { |c,h| h[c] = status_description(string, c) } #=> {"green" =>[], # "yellow"=>[ # "A load balancer has no listener that uses a secure protocol (HTTPS or SSL).", # "A load balancer listener uses an outdated predefined SSL security policy.", # "A load balancer listener uses a cipher or protocol that is not recommended." # ], # "red"=>["A load balancer listener uses an insecure cipher or protocol."] # }
您可以执行以下操作来提取包含"建议操作"的句子.1
r0 = / \nRecommended\sAction<\/b>\n
- # match string \K # discard everything matched so far .+? # match any number of any character, lazily (?) (?=<\/li>\s<\/ul>) # match string /mx # multiline and free-spacing regex definition modes r1 = /<\/li>\s*\n\s*
- / # match string
string[r0].split(r1) #=> ["If the traffic to your load balancer must be secure, use either the \ # HTTPS or the SSL protocol for the front-end connection.", # "Upgrade your load balancer to the latest version of the predefined \ # SSL security policy.", # "Use only the recommended ciphers and protocols."]
注意
string[r0] #=> "If the traffic to your load balancer must be secure, use either \ # the HTTPS or the SSL protocol for the front-end connection.\ # \n
1.在构建r0
我在取代了单一的空间"Recommended Action"
和"(?=<\/li> <\/ul>)"
与\s
.只有在自由间隔模式(/x
)中定义正则表达式时才需要这样做,它忽略空格.此外,\nRecommended\sAction<\/b>\n
可以用积极的lookbehind代替:(?<=\nRecommended\sAction<\/b>\n
.最后,我格式化了返回字符串,因此无需水平滚动即可读取它们.