拆分不含非字符的字符串

发布于 2025-01-04 17:31:01 字数 208 浏览 0 评论 0原文

我正在尝试分割一个看起来像这样的字符串,例如:

':foo [bar]'

对此使用 str.split() 当然会返回 [':foo','[bar]']

我怎样才能让它返回只包含这些字符的 ['foo','bar'] 呢?

I'm trying to split a string that looks like this for example:

':foo [bar]'

Using str.split() on this of course returns [':foo','[bar]']

But how can I make it return just ['foo','bar'] containing only these characters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅听莫相离 2025-01-11 17:31:03

我不喜欢正则表达式,但喜欢 Python,所以我可能会写成

>>> s = ':foo [bar]'
>>> ''.join(c for c in s if c.isalnum() or c.isspace())
'foo bar'
>>> ''.join(c for c in s if c.isalnum() or c.isspace()).split()
['foo', 'bar']

“”.join 习惯用法有点奇怪,我承认,但你几乎可以用英语阅读其余部分:“join every character for如果字符是字母数字或字符是空格,则分割 s 中的字符,然后将其拆分”。

或者,如果您知道要删除的符号始终位于外部,并且单词仍将由空格分隔,并且您知道它们是什么,您可以尝试类似的操作

>>> s = ':foo [bar]'
>>> s.split()
[':foo', '[bar]']
>>> [word.strip(':[]') for word in s.split()]
['foo', 'bar']

I don't like regular expressions, but do like Python, so I'd probably write this as

>>> s = ':foo [bar]'
>>> ''.join(c for c in s if c.isalnum() or c.isspace())
'foo bar'
>>> ''.join(c for c in s if c.isalnum() or c.isspace()).split()
['foo', 'bar']

The ''.join idiom is a little strange, I admit, but you can almost read the rest in English: "join every character for the characters in s if the character is alphanumeric or the character is whitespace, and then split that".

Alternatively, if you know that the symbols you want to remove will always be on the outside and the word will still be separated by spaces, and you know what they are, you might try something like

>>> s = ':foo [bar]'
>>> s.split()
[':foo', '[bar]']
>>> [word.strip(':[]') for word in s.split()]
['foo', 'bar']
故事未完 2025-01-11 17:31:03

照常执行 str.split(),然后解析每个元素以删除非字母。像这样的东西:

>>> my_string = ':foo [bar]'
>>> parts = [''.join(c for c in s if c.isalpha()) for s in my_string.split()]
['foo', 'bar']

Do str.split() as normal, and then parse each element to remove the non-letters. Something like:

>>> my_string = ':foo [bar]'
>>> parts = [''.join(c for c in s if c.isalpha()) for s in my_string.split()]
['foo', 'bar']
七色彩虹 2025-01-11 17:31:03

您必须使用正则表达式遍历列表 ['foo','[bar]'] 并删除所有非字母字符。检查正则表达式替换(在Python中)-更简单的方法?示例和文档参考。

You'll have to pass through the list ['foo','[bar]'] and strip out all non-letter characters, using regular expressions. Check Regex replace (in Python) - a simpler way? for examples and references to documentation.

酷遇一生 2025-01-11 17:31:03

您必须尝试正则表达式

使用 re.sub() 替换 :,[, ] 字符,然后使用 空格 作为分隔符分割结果字符串。

>>> st = ':foo [bar]'
>>> import re
>>> new_st = re.sub(r'[\[\]:]','',st)
>>> new_st.split(' ')
['foo', 'bar']

You have to try regular expressions.

Use re.sub() to replace :,[,] characters and than split your resultant string with white space as delimiter.

>>> st = ':foo [bar]'
>>> import re
>>> new_st = re.sub(r'[\[\]:]','',st)
>>> new_st.split(' ')
['foo', 'bar']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文