Calculate sum of all numbers present in a string using Regex in Python
The idea is to use inbuilt function Python RegEx.
Below is the Implementation of above approach:
C++14
#include <iostream> #include <regex> // Function to calculate sum of all // numbers present in a string // containing alphanumeric characters int findSum(std::string str) { // Regular Expression that matches // digits in between a string std::regex pattern( "\\d+" ); std::smatch match; int sum = 0; while (std::regex_search(str, match, pattern)) { sum += stoi(match[0].str()); str = match.suffix().str(); } return sum; } // Driver code int main() { // input alphanumeric string std::string str = "12abc20yz68" ; // Function call std::cout << findSum(str) << std::endl; return 0; } // This code is contributed by Shivam Tiwari |
Python3
# Python3 program to calculate sum of # all numbers present in a string # containing alphanumeric characters # Function to calculate sum of all # numbers present in a string # containing alphanumeric characters import re def find_sum(str1): # Regular Expression that matches # digits in between a string return sum ( map ( int , re.findall( '\d+' , str1))) # Driver code # input alphanumeric string str1 = "12abc20yz68" # Function call print (find_sum(str1)) # This code is contributed # by Venkata Ramana B |
Javascript
// JavaScript program to calculate sum of // all numbers present in a string // containing alphanumeric characters // Function to calculate sum of all // numbers present in a string // containing alphanumeric characters function find_sum(str1) { // Regular Expression that matches // digits in between a string return str1.match(/\d+/g).reduce((acc, val) => acc + parseInt(val), 0); } // Driver code // input alphanumeric string const str1 = "12abc20yz68" ; // Function call console.log(find_sum(str1)); |
Java
import java.util.regex.*; public class Main { // Function to calculate sum of all // numbers present in a string // containing alphanumeric characters public static int findSum(String str) { // Regular Expression that matches // digits in between a string Pattern pattern = Pattern.compile( "\\d+" ); Matcher matcher = pattern.matcher(str); int sum = 0 ; while (matcher.find()) { sum += Integer.parseInt(matcher.group()); str = matcher.replaceFirst( "" ); matcher = pattern.matcher(str); } return sum; } // Driver code public static void main(String[] args) { // input alphanumeric string String str = "12abc20yz68" ; // Function call System.out.println(findSum(str)); } } |
C#
using System; using System.Text.RegularExpressions; public class GFG { // Function to calculate sum of all // numbers present in a string // containing alphanumeric characters public static int FindSum( string str) { // Regular Expression that matches // digits in between a string Regex pattern = new Regex( @"\d+" ); Match matcher = pattern.Match(str); int sum = 0; while (matcher.Success) { sum += Int32.Parse(matcher.Value); str = pattern.Replace(str, "" , 1, matcher.Index); matcher = pattern.Match(str); } return sum; } // Main method static public void Main() { // input alphanumeric string string str = "12abc20yz68" ; // Function call Console.WriteLine(FindSum(str)); } } |
Output
100
Time complexity: O(n) where n is length of the string.
Auxiliary Space: O(n) where n is length of the string.
Calculate sum of all numbers present in a string
Given a string S containing alphanumeric characters, The task is to calculate the sum of all numbers present in the string.
Examples:
Input: 1abc23
Output: 24
Explanation: 1 + 23 = 24Input: geeks4geeks
Output: 4Input: 1abc2x30yz67
Output: 100
Contact Us