A have a series of strings in the following format. Demonstration examples would look like this:
71 1 * abwhf
8 askg
*14 snbsb
00ab
I am attempting to write a Python 3 program that will use a for loop to cycle through each string and split it once at the first occurrence of a letter into a list with two elements.
The output for the strings above would become lists with the following elements:
71 1 * and abwhf
8and askg
*14 and snbsb
00 and ab
There is supposed to be a space after the first string of the first three examples but this only shows in the editor
How can I split the string in this way?
Two posts look of relevance here:
The first answer for the first question allows me to split a string at the first occurrence of a single character but not multiple characters (like all the letters of the alphabet).
The second allows me to split at the first letter, but not just one time. Using this would result in an array with many elements.
解决方案
Using re.search:
import re
strs = ["71 1 * abwhf", "8 askg", "*14 snbsb", "00ab"]
def split_on_letter(s):
match = re.compile("[^\W\d]").search(s)
return [s[:match.start()], s[match.start():]]
for s in strs:
print split_on_letter(s)
The regex [^\W\d] matches all alphabetical characters.
\W matches all non-alphanumeric characters and \d matches all numeric characters. ^ at the beginning of the set inverts the selection to match everything that is not (non-alphanumeric or numeric), which corresponds to all letters.
match searches the string to find the index of the first occurrence of the matching expression. You can slice the original string based on the location of the match to get two lists.