In this tutorial, we are going to write a regex that finds the most occurring number in the string. We will check the regex in Python.
Follow the below steps to write the program.
# importing the modules import re import collections # initializing the string string = '1222tutorials321232point3442' # regex to find all the numbers regex = r'[0-9]' # getting all the numbers from the string numbers = re.findall(regex, string) # counter object counter = collections.Counter(numbers) # finding the most occurring number high_frequency = 0 highest_frequency_number = None for key in list(counter.keys()): if counter[key] > high_frequency: highest_frequency_number = counter[key] # printing the number print(highest_frequency_number)
If you run the above code, then you will get the following result.
If you have any queries in the tutorial, mention them in the comment section.