Skip to content

Commit fcbec33

Browse files
committed
fix: handle non-ASCII characters in extract_alphanumeric
1 parent bc19e68 commit fcbec33

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/basic/string.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def reverse_string(text: str) -> str:
2020
return text[::-1]
2121

2222

23-
def extract_alphanumeric(text: str) -> str:
23+
def extract_alphanumeric(s: str) -> str:
2424
"""
2525
Extracts only alphanumeric characters from the given string
2626
2727
Args:
28-
text (str): The input string
28+
s (str): The input string
2929
3030
Returns:
3131
str: String containing only alphanumeric characters
@@ -35,5 +35,9 @@ def extract_alphanumeric(text: str) -> str:
3535
'HelloWorld123'
3636
>>> extract_alphanumeric("@#$% abc 123")
3737
'abc123'
38+
>>> extract_alphanumeric("こんにちは123ABCワールド")
39+
'123ABC'
3840
"""
39-
return "".join(char for char in text if char.isalnum())
41+
# isascii() ensures we only get ASCII characters (0-127),
42+
# excluding Unicode characters like Japanese, Emoji, etc.
43+
return "".join(c for c in s if c.isascii() and c.isalnum())

0 commit comments

Comments
 (0)