Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
下面的代码让用户输入两个由&,|或分隔的电影名称^:
&
|
^
query = raw_input("Enter your query:") movie_f = re.split('&|\^|\|', query)[0].strip() movie_s = re.split('&|\^|\|', query)[1].strip()
我想知道用什么re来分隔字符串(&或|)^。我怎样才能做到这一点?
re
如果您对正则表达式进行分组,它将为每隔一个项目返回它拆分的项目。
>>> query '&foo^bar' >>> re.split(r'(&|\^|)', query) ['', '&', 'foo', '^', 'bar']