- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The most occurring number in a string using Regex in python
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.
- Import the re and collections modules.
- Initialize the string with numbers.
- 4Find all the numbers using regex and store them in the array.
- Find the most occurring number using Counter from collections module.
# 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.
- Program to find most occurring number after k increments in python
- How to Identify Most Frequently Occurring Items in a Sequence with Python?
- Python program to find the most occurring character and its count
- Get the count of the most frequently occurring values in MySQL?
- How to obtain the highest occurring character in a String using C#?
- Count top 10 most occurring values in a column in MySQL?
- Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python
- Maximum occurring character in an input string using C++
- Count the number of vowels occurring in all the substrings of given string in C++
- Python program to find the number occurring odd number of times using Lambda expression and reduce function
- Extracting each word from a string using Regex in Java
- Find all the patterns of "10+1" in a given string using Python Regex
- Python Program to check if a string starts with a substring using regex
- Split string into sentences using regex in PHP