regular expressions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

regular expressions

How to use regular expressions to find such sequences of numbers in the text: "8 9 10" or "10 10 9" or "9 9 8" or "9 8 10"?

12th Apr 2021, 5:37 PM
Mary_d9
Mary_d9 - avatar
6 Answers
+ 1
Ok. So, to make sure, for the string 'four 3 8 10 six 9 10 8 8' the correct output would be '8 10', '9 10 8 8' Is that correct? If so, '((?:(?:8|9|10)\s)*(?:8|9|10))' appears to do the trick. Not extensively tested though.
12th Apr 2021, 7:49 PM
Russ
Russ - avatar
+ 2
This can be done exactly as you said in the question.
12th Apr 2021, 5:53 PM
JaScript
JaScript - avatar
+ 2
This is a short code for test of your input Mary_d9 with in your question mentioned requested sequences: https://code.sololearn.com/W464rI07K52h/?ref=app
12th Apr 2021, 8:43 PM
JaScript
JaScript - avatar
0
Two questions: will there always be 3 numbers, or will the number vary, and what language are you using?
12th Apr 2021, 7:15 PM
Russ
Russ - avatar
0
Russ The text can contain different numbers and even dates, but I only need 8, 9 and 10. There is a space separator between them. I will execute the command with regex in bash.
12th Apr 2021, 7:27 PM
Mary_d9
Mary_d9 - avatar
0
Russ thank you!
12th Apr 2021, 7:52 PM
Mary_d9
Mary_d9 - avatar