嗨,我有如下字串,
apiVersion: v1
clusters:
- cluster: <------------------------ this is one cluster
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ
server: https://api.someaddress
name: https://api.anotheraddress
- second_cluster: <------------------------ this is second cluster
certificate-authority-data: LS0tLS1
server: https://api.someaddress
name: https://api.
contexts:
- context: <------------------------ this is one context
cluster: https://api.another1address
namespace: name1
user: user1
name: somename
- another_context: <------------------------ this is second context
cluster: https://api.anotheraddress
namespace: esm2
user: admin
name: somename1
current-context: some-context
kind: somekind
preferences: {}
users:
- name: user-admin
user:
token: eyJhb
我如何使用正則運算式檢查集群下是否有多個集群以及背景關系下是否存在多個背景關系。
我已經嘗試過類似下面的東西
var matchServer = new RegExp(/- cluster:\n[A-Za-z0-9\- :]*\n[ ] server: ([A-Za-z:.//]*)/, "gi").exec(s);
const namespace = s.match(/contexts:\n- context:\n[A-Za-z0-9\- :.//]*\n[ ] namespace: ([A-Za-z0-9]*)/i);
但這給了服務器和命名空間也只回傳了一次。
我想在集群和背景關系下獲取匹配字串“集群”的數量。
有人可以幫我解決這個問題。我是編程新手。謝謝。
我試過什么?
我已經嘗試過正則運算式來匹配背景關系下的“背景關系”“another_context”。但它只匹配“背景關系”
背景關系:\n-.*背景關系:
有人可以幫我理解為什么它不匹配 another_context 字串
uj5u.com熱心網友回復:
<html>
<head>
<script>
var s = `apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ
server: https://api.someaddress1
name: https://api.anotheraddress
- second_cluster:
certificate-authority-data: LS0tLS1
server: https://api.someaddress2
name: https://api.
contexts:
- context:
cluster: https://api.another1address
namespace: name1
user: user1
name: somename
- another_context:
cluster: https://api.anotheraddress
namespace: esm2
user: admin
name: somename1
current-context: some-context
kind: somekind
preferences: {}
users:
- name: user-admin
user:
token: eyJhb`;
var matchServer = s.match(/- [a-z_]*cluster:/gi)
var matchNamespace = s.match(/- [a-z_]*context:/gi)
document.write(matchServer.length ' servers <br/>')
for(i=0;i<matchServer.length;i )
document.write(matchServer[i] '<br/>')
document.write(matchNamespace.length ' namespaces <br/>')
for(i=0;i<matchNamespace.length;i )
document.write(matchNamespace[i] '<br/>')
</script>
</head>
<body>
</body>
</html>
js小提琴
uj5u.com熱心網友回復:
為了完整起見,這里有一個帶有YAML 決議器的解決方案——前提是它實際上是 YAML,因為最后一部分 ( users) 不是有效的 YAML。還有一些縮進是不對的。我認為它已被修改為不泄漏敏感資訊。
更新:哦,我看到這是一個 kubernetes 組態檔。所以它真的是 YAML。
https://jsfiddle.net/1Lpsk740/(用document.write代替console.log)
<!DOCTYPE html>
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.js"></script>
<script>
var input = `apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ
server: https://api.someaddress1
name: https://api.anotheraddress
- second_cluster:
certificate-authority-data: LS0tLS1
server: https://api.someaddress2
name: https://api.
contexts:
- context:
cluster: https://api.another1address
namespace: name1
user: user1
name: somename
- another_context:
cluster: https://api.anotheraddress
namespace: esm2
user: admin
name: somename1`;
const doc = jsyaml.load(input);
console.log('servers: ' doc.clusters.length);
doc.clusters.forEach(e => console.log('- ' Object.keys(e)[0]));
console.log('namespaces: ' doc.contexts.length);
doc.contexts.forEach(e => console.log('- ' Object.keys(e)[0]));
</script>
</html>
輸出:
servers: 2
- cluster
- second_cluster
namespaces: 2
- context
- another_context
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/432907.html
標籤:正则表达式
下一篇:拆分文本和數字
