admin管理员组文章数量:1334887
I am trying to validate aws arn for a connect instance but I am stuck on creating the correct regex.
Below is the string that I want to validate.
arn:aws:connect:us-west-2:123456789011:instance/0533yu22-d4cb-410a-81da-6c9hjhjucec4b9
I want to create a regex which checks below things.
arn:aws:connect:<region_name>:<12 digit account id>:instance/<an alphanumeric instance id>
Can someone please help.
Tried below
^arn:aws:connect:\S+:\d+:instance\/\S+\/queue\/\S+$
I am trying to validate aws arn for a connect instance but I am stuck on creating the correct regex.
Below is the string that I want to validate.
arn:aws:connect:us-west-2:123456789011:instance/0533yu22-d4cb-410a-81da-6c9hjhjucec4b9
I want to create a regex which checks below things.
arn:aws:connect:<region_name>:<12 digit account id>:instance/<an alphanumeric instance id>
Can someone please help.
Tried below
^arn:aws:connect:\S+:\d+:instance\/\S+\/queue\/\S+$
Share
Improve this question
edited Dec 5, 2024 at 9:16
Yves M.
31k24 gold badges109 silver badges149 bronze badges
asked Jan 16, 2022 at 16:32
NaxiNaxi
2,0627 gold badges45 silver badges98 bronze badges
3
- 2 What effort have you made? – Daniel A. White Commented Jan 16, 2022 at 16:38
-
already for the fist matching sequence the OP does not want to match non whitespace characters since
:
would be included, but the OP wants to match anything which is not a:
... thus([^:]+):
instead of\S+
– Peter Seliger Commented Jan 16, 2022 at 16:48 -
2
^(?:\w+:){3}(?<region>[^:]+):(?<accountId>\d{12}):instance\/(?<instanceId>.*)$
– Peter Seliger Commented Jan 16, 2022 at 16:56
2 Answers
Reset to default 1There is no /queue/
substring in your example string, and \S+
matches any no whitespace character and will cause backtracking to match the rest of the pattern.
You might update your pattern to ^arn:aws:connect:\S+:\d+:instance\/\S+$
but that will be less precise according to the things you want to check.
A bit more precise pattern could be:
^arn:aws:connect:\w+(?:-\w+)+:\d{12}:instance\/[A-Za-z0-9]+(?:-[A-Za-z0-9]+)+$
^
Start of stringarn:aws:connect:
Match literally\w+(?:-\w+)+:
Match 1+ word characters and repeat matching-
and 1+ word characters and then match:
\d{12}:
Match 12 digits and:
instance\/
Matchinstance/
[A-Za-z0-9]+(?:-[A-Za-z0-9]+)+
Match 1+ alpha numerics and repeat 1+ times-
and 1+ alpha numerics$
End of string
Regex demo
You need some capture groups to facilitate this. Here I've also used named capture groups for ease of understanding.
const string = "arn:aws:connect:us-west-2:123456789011:instance/0533yu22-d4cb-410a-81da-6c9hjhjucec4b9";
// Regex broken down into parts
const parts = [
'arn:aws:connect:',
'(?<region_name>[^:]+?)', // group 1
':',
'(?<account_id>\\d{12})', // group 2
':instance\\/',
'(?<instance_id>[A-z0-9\\-]+?)', // group 3
'$'
];
// Joined parts into regex expression
const regex = new RegExp(parts.join(''));
// Execute query and assign group values to variables
const { region_name, account_id, instance_id } = regex.exec(string).groups;
console.log("region_name:", region_name);
console.log("account_id:", account_id);
console.log("instance_id:", instance_id);
本文标签: javascriptRegex to match an aws arnStack Overflow
版权声明:本文标题:javascript - Regex to match an aws arn - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374783a2462944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论