Signup/Sign In

Find most occurring number in a string using Regex in python

In this tutorial, we will learn to find the most occurring number in a string using Regex in python.

Here, we will take a string with some numbers, and then we will find the number which has occurred a maximum number of times. We will use regex to do so.

Python Regex also called REs or regular expressions is a module using which we can specify rules to set the possible strings to match. It is available in re module.

Look at the examples to understand the input and output format.

Input:'ABC12ED13we14'

Output: max_value: 1

Input:'ab12342A22'

Output: max_value: 2

To find a number occurring most of the time, we will use the following approach.

We will use re.findall() method to get the list of numbers in the string and then use a counter to track each occurrence of the number. The number with the highest occurrence will be the resultant output.

Approach: Using findall() and counter() method

In this method, we will find the numbers within the input string using re.findall() method. Then we will count the occurrence of each number using a counter() from the collection module.

Algorithm

Step1: Import re module and collection module

Step2: Define a regex expression of number.

Step3: Use the regex method to list out the number in the string.

Step4: Use counter() method to make a list of counts for each number.

Step5: Initialize max_count with 0 and max_value with None

Step6: Iterate each item of count

step7: Compare the counter key with max_count

step8: If the max_count is less than the counter key update the max_count and store the value to max_value

Step9: The item with max_value is the resultant output.

Python Program 1

Here is a python program to describe the above steps for finding the number with maximum occurrence.

# import module for regular expression and collections
import re
import collections

string='ABC12ED13we14'

number = re.findall(r"[0-9]", string)

counter = collections.Counter(number)
max_count = 0
max_value = None
for key in list(counter.keys()):
  if(counter[key]>max_count):
    max_count = counter[key]
    max_value = int(key)
print("max_value",max_value)

Output

Here is the output of the above code.

max value

Python Program 2

Here is a python program to describe the above steps for finding the number with maximum occurrence.

# import module for regular expression and collections
import re
import collections

string='ab12342A22'

number = re.findall(r"[0-9]", string)

counter = collections.Counter(number)
max_count = 0
max_value = None
for key in list(counter.keys()):
  if(counter[key]>max_count):
    max_count = counter[key]
    max_value = int(key)
print("max_value",max_value)

Output

Here is the output of the above code.

max value

Conclusion

In this tutorial, we have learned to find the number in the given string with the most occurrence. To do so we have used regex along with the collection module.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.