I want to get array number from string. Example:
Input: "@[Anonymous:2] abccdef, @[Sales:5]"
Output: [2, 5]
Please help me. Thanks
I don't know what language you use but you can leave a comment and I would gladly update my answer for you and help. Edit: The OP specified javascript in the comments below.
@\[\w+:(\d+)\]
The result for each match is in capture group 1
You can check your input and regex at regex.com
var s = "@[Anonymous:2] abccdef, @[Sales:5]"
var r = /@\[\w+:(\d+)\]/g
var m = r.exec(s)
var a = []
while (m != null) {
a.push(Number(m[1]))
m = r.exec(s)
}
console.log(a)
@\[\w+:(\d+)\] performs slightly better and also captures the whole number (your current regex will only capture the last digit of a two or more digit number)+ was not in the capture group - whilst it should). Notice that SO has the snippet functionality and also includes a builtin display for the console.[^:\]]+ assuming only one colon and right square bracket is possible within the []