Python program to Extract string till first Non-Alphanumeric character
Last Updated :
22 Mar, 2023
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
import re
test_str = 'geeks4g!!!eeks'
print ( "The original string is : " + str (test_str))
res = re.search(r '\W+' , test_str).start()
res = test_str[ 0 : res]
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
import re
test_str = 'geeks4g!!!eeks'
print ( "The original string is : " + str (test_str))
res = re.findall( "[\dA-Za-z]*" , test_str)[ 0 ]
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
- Initiate a for loop to access characters of string
- If the character is not alphanumeric append characters to output string
- If the character is alphanumeric break the loop
- Display the output string
Python3
test_str = 'geeks4g!!!eeks'
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
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
test_str = 'geeks4g!!!eeks'
print ( "The original string is : " + str (test_str))
for i in range ( len (test_str)):
if not test_str[i].isalnum():
res = test_str[:i]
break
else :
res = test_str
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
def extract_string(string):
if not string or not string[ 0 ].isalnum():
return ""
return string[ 0 ] + extract_string(string[ 1 :])
test_str = 'geeks4g!!!eeks'
print ( "The original string is : " + str (test_str))
res = extract_string(test_str)
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...