Open In App

Python program to Extract string till first Non-Alphanumeric character

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string, extract all the alphanumerics before 1st occurrence of non-alphanumeric.

Input : test_str = ‘geek$s4g!!!eeks’ 
Output : geek 
Explanation : Stopped at $ occurrence.

Input : test_str = ‘ge)eks4g!!!eeks’ 
Output : ge 
Explanation : Stopped at ) occurrence.

Method #1 : Using regex + search()

In this, search() is used to search appropriate regex() for alphanumerics, then the result is sliced till 1st occurrence of a non-alphanumeric character

Python3




# Python3 code to demonstrate working of
# Extract string till first Non-Alphanumeric character
# Using regex + search()
import re
 
# initializing string
test_str = 'geeks4g!!!eeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using start() to get 1st substring
res = re.search(r'\W+', test_str).start()
res = test_str[0 : res]
         
# printing result
print("The resultant string : " + str(res))


Output

The original string is : geeks4g!!!eeks
The resultant string : geeks4g

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using findall()

This is yet another regex way to solve this problem. In this, we extract the 1st substring before non-alnum character by accessing the 0th index.

Python3




# Python3 code to demonstrate working of
# Extract string till first Non-Alphanumeric character
# Using findall()
import re
 
# initializing string
test_str = 'geeks4g!!!eeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using findall() to get all substrings
# 0th index gives 1st substring
res = re.findall("[\dA-Za-z]*", test_str)[0]
         
# printing result
print("The resultant string : " + str(res))


Output

The original string is : geeks4g!!!eeks
The resultant string : geeks4g

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using for loops

Approach

  1. Initiate a for loop to access characters of string
  2. If the character is not alphanumeric append characters to output string
  3. If the character is alphanumeric break the loop
  4. Display the output string

Python3




# Python3 code to demonstrate working of
# Extract string till first Non-Alphanumeric character
 
# initializing string
test_str = 'geeks4g!!!eeks'
 
# printing original string
print("The original string is : " + str(test_str))
alphabets="abcdefghijklmnopqrstuvwxyz"
digits="0123456789"
res=""
for i in test_str:
    if i not in alphabets+digits:
        break
    else:
        res+=i
# printing result
print("The resultant string : " + str(res))


Output

The original string is : geeks4g!!!eeks
The resultant string : geeks4g

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #4: Using string slicing

This method uses a for loop to iterate through the characters in the string, and checks if each character is alphanumeric using the isalnum() method. If it encounters a non-alphanumeric character, it extracts the substring of the original string up to that character using string slicing (test_str[:i]). If there are no non-alphanumeric characters in the string, it simply returns the original string.

Python3




# Python3 code to demonstrate working of
# Extract string till first Non-Alphanumeric character
 
# initializing string
test_str = 'geeks4g!!!eeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Method #4: Using string slicing
for i in range(len(test_str)):
    if not test_str[i].isalnum():
        res = test_str[:i]
        break
else:
    res = test_str
 
# printing result
print("The resultant string : " + str(res))


Output

The original string is : geeks4g!!!eeks
The resultant string : geeks4g

Time Complexity : O(N), where N is length of test_str
Auxiliary Space : O(N)

Method#5: Using Recursive method.

This implementation uses a recursive function extract_string that takes in a string as input and returns the extracted string. If the input string is empty or the first character is non-alphanumeric, it returns an empty string. Otherwise, it returns the first character concatenated with the result of calling the function recursively with the remaining substring.

Python3




# Python3 code to demonstrate working of
# Extract string till first Non-Alphanumeric character
def extract_string(string):
    if not string or not string[0].isalnum():
        return ""
    return string[0] + extract_string(string[1:])
 
# initializing string
test_str = 'geeks4g!!!eeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
res = extract_string(test_str)
# printing result
print("The resultant string : " + str(res))


Output

The original string is : geeks4g!!!eeks
The resultant string : geeks4g

Note that this implementation can be inefficient for very long input strings, as it creates a new substring on each recursive call. 
The time complexity is O(n) where n is the length of the input string. 
The space complexity is also O(n) due to the recursive call stack.



Last Updated : 22 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads