Question: A string is given which will contain single digit numbers, letters, and question marks, and you have to check that string contains ???
between every pair of two numbers and their sum needs to be 10. If so then return true otherwise return false.
Understand it with the example:
a) arrb6???9xxbl5???eee5: true [because each pair contains `???` and the sum of any 2 pair digits are 10, (10!= 6+9 but 10 == 5+5)]
b) arrb6???9xxbl5???eee9: false [because each pair contains `???` but the sum of any 2 pair digits are not 10, (10!= 6+9 and 10 != 5+9)]
c) arrb6???9xxbl5??eee59: false [because each pair doesn't contain `???`, so straightforward false]
The jQuery code is already written for it:
The equivalent PHP code is required.
2
Answers
The equivalent PHP code is given below:
Sample Output: https://3v4l.org/lv4so
You can make use of the below regex that uses
positive lookahead
:It basically means to match a set of digits and non-digit letters only if they are followed by a set of digits later. We can then process each pair of digits in their own captured groups and assert if
???
exists in our captured group of non-digit characters and that if digits add up to 10.Online Demo