Is it possible to turn groups in a regex match into variables? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is it possible to turn groups in a regex match into variables?

Let's say I have a string: "15m8s" I want my 2 variables (min, sec) to be 15 and 8 So I have this regular expression: /(\d+)m(\d+)s/ Notice how it has 2 groups. When matched to the string, the matched groups will be: "15" and "8" Is there any way to make a variable out of each group when matched? Answers will be most helpful in JS or Kotlin, but I dont really care which language you are gonna use, I just want to know if it is possible in any language. I know it is a really confusing question and I dont think I'm that good at explaining it so if you dont understand me, feel free to ask questions

18th Aug 2018, 3:35 PM
ReimarPB
ReimarPB - avatar
5 Answers
18th Aug 2018, 3:44 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 5
I have a JavaScript example: use input.match(/(\d+)m(\d+)s/) https://code.sololearn.com/WJFPfaTs7afI/?ref=app
19th Aug 2018, 12:33 AM
Calviղ
Calviղ - avatar
+ 1
Ulisses Cruz Thank you! Now I know it's possible. I'll read through the kotlin and js docs to see if I can find anything
18th Aug 2018, 3:48 PM
ReimarPB
ReimarPB - avatar
+ 1
Oh wow, I already found out how to do it in Kotlin. Thanks for the help https://code.sololearn.com/c4VBFTbN6MMW/?ref=app
18th Aug 2018, 4:02 PM
ReimarPB
ReimarPB - avatar
+ 1
Calviղ thanks!
19th Aug 2018, 6:54 AM
ReimarPB
ReimarPB - avatar