简体   繁体   中英

Regular expression to split() the string into 2 parts. Any letters and everything after

Input string, output - list with 2 strings:
A few examples:

str1 = dir/?
str2 = dir c:\users /A/B
str3 = dir>1.txt

list1 = ['dir', '/?']
list2 = ['dir', ' c:\users /A/B'] #with space
list3 = ['dir', '>1.txt']

r"([a-zA-Z_]+)(.*)" should do it, though I'm confused about the presence of the underscore in #4, since you wrote "any letters" in the title.

to actually use this in code:

import re
str1 = "dir/?"
m = re.match(r"([a-zA-Z_]+)(.*)", str1)
list1 = list(m.groups()) #list1 is now ['dir', '/?'], as desired

If you don't actually want the underscore, then just remove it from the first character class: r"([a-zA-Z]+)(.*)"

Try this.

str1 = 'dir/?'
j = s[:str1.index('r')+1:],s[str1.index('r')+1:]
list1 = list(j)

Also try.

list1 = re.split(r'([a-zA-Z]+)',str1)[1:]

It might be helpful for you to include a bit more info, but in Python, you could avoid regular expressions by using the .split() function and limit the output to 2 parts:

str = "dir c:\\users /A/B"
str.split(" ", 2)
# ['dir', 'c:\\users', '/A/B']

If you have to field the possibility of no spaces, you could use lstrip to remove the prefix and then remove any whitespace.

str.lstrip('dir').lstrip()    

Note the extra backslash to escape the backslash in the string.

Regular expressions are more flexible for sure, but sometimes the simpler solutions can be appropriate depending on your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM