如何从git repo URI中提取主机名?

时间:2017-03-29 06:22:40

标签: ruby chef

给出git存储库URI Like

Use Trait variations

git@company-name.com:/company-name/repo.git

如何从中提取https://company-name.com/company-name/repo.git ?我正在尝试获取主机名,因此我可以将它们传递给名为company-name.com的食谱。

2 个答案:

答案 0 :(得分:1)

您可以使用正则表达式从网址(regex101.com)中提取主机名。此表达式将匹配任何用户和两个协议(http / https)。

{"_index":"pos_journals-2017.03.24","_type":"pos_journals","_id":"AVr_peEqanqtRFPuszUt","_score":null,"_source": {"message":"Chk 4040                   Gst 0\r\n1005 Yangshuying          PCWS02\r\nCE:   1005 CC:      0 TC:      0\r\nTrn 8704          24Mar'17 17:28\r\n--------------------------------\r\n    To Go       \r\n      SR:6010381353541922\r\n           SR:ONLINE\r\n  1 Mocha Ice V         37.00\r\n    Cash               102.00\r\n    Subtotal            37.00\r\n    Paid                37.00\r\n    Change Due          65.00\r\n================================","@version":"1","@timestamp":"2017-03-24T09:28:56.787Z","type":"pos_journals","count":1,"beat":{"hostname":"S24376","name":"S24376"},"source":"d:\\MICROS\\Res\\Pos\\Journals\\POS02.txt","offset":40621,"input_type":"log","fields":null,"host":"S24376","tags":["beats_input_codec_plain_applied"]},"fields":{"@timestamp":[1490347736787]},"highlight":{"message":["Chk 4040                   Gst 0\r\n1005 Yangshuying          PCWS02\r\nCE:   1005 CC:      0 TC:      0\r\nTrn 8704          24Mar'17 17:28\r\n--------------------------------\r\n    To Go       \r\n      SR:6010381353541922\r\n           SR:ONLINE\r\n  1 Mocha Ice V         37.00\r\n    @kibana-highlighted-field@Cash@/kibana-highlighted-field@               102.00\r\n    Subtotal            37.00\r\n    Paid                37.00\r\n    Change Due          65.00\r\n================================"]},"sort":[1490347736787]}

 0   {"_index":"pos_journals-2017.03.24","_type":"p...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 1   {"_index":"pos_journals-2017.03.24","_type":"p...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 2   {"_index":"pos_journals-2017.03.24","_type":"p...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 3   {"_index":"pos_journals-2017.03.24","_type":"p...   

用法:

[a-zA-Z0-9_]+@(.+?):|https?:\/\/(.+?)\/

答案 1 :(得分:1)

使用适当的工具。 URI解析是一个已解决的问题。

require 'uri'

s = 'https://company-name.com/company-name/repo.git'
uri = URI(s)
uri.host # => "company-name.com"