0

I want to get array number from string. Example:

Input: "@[Anonymous:2] abccdef, @[Sales:5]"

Output: [2, 5]

Please help me. Thanks

1 Answer 1

2

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 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)

Sign up to request clarification or add additional context in comments.

7 Comments

@\[\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)
@ctwheels, thanx, check it now, you can run it here, it is only code snippet to help debugging his results
@msoliman I've simplified your Javascript and regex pattern (you don't need to escape everything in regex, plus the + 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.
@ctwheels thanx, appreciated
@TrầnQuốcVũ you can use [^:\]]+ assuming only one colon and right square bracket is possible within the []
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.