在Python中分割字符串
我有一个像这样的字符串:
this is [bracket test] "andquotes test "
我正在尝试用 Python 编写一些内容,以按空格分隔它,同时忽略方括号和引号内的空格。 我正在寻找的结果是:
['this','is','括号测试','和引号测试']
I have a string which is like this:
this is [bracket test] "and quotes test "
I'm trying to write something in Python to split it up by space while ignoring spaces within square braces and quotes. The result I'm looking for is:
['this','is','bracket test','and quotes test ']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个适用于您的测试输入的简单解决方案:
匹配的任何代码。
这适用于您的示例,但对于您可能遇到的许多实际字符串可能会失败。 例如,您没有说明您对不平衡的括号或引号的期望,或者您希望单引号或转义字符如何工作。 不过,对于简单的情况,上面的内容可能就足够了。
Here's a simplistic solution that works with your test input:
This will return any code that matches either
This works with your example, but might fail for many real-world strings you may encounter. For example, you didn't say what you expect with unbalanced brackets or quotes,or how you want single quotes or escape characters to work. For simple cases, though, the above might be good enough.
要完成 Bryan 帖子并完全匹配答案:
不要误解所使用的整个语法:这不是单行上的多个语句,而是单个功能语句(更防错误)。
To complete Bryan post and match exactly the answer :
Don't misunderstand the whole syntax used : This is not several statments on a single line but a single functional statment (more bugproof).
这是一个简单的解析器(根据示例输入进行测试),它引入了状态设计模式。
在现实世界中,您可能希望使用 PLY 之类的东西构建一个真正的解析器。
Here's a simplistic parser (tested against your example input) that introduces the State design pattern.
In real world, you probably want to build a real parser using something like PLY.
这是一种更程序化的方法:
Here's a more procedural approach:
好吧,我已经多次遇到这个问题,这促使我编写自己的系统来解析任何类型的语法。
结果可以在此处找到; 请注意,这可能有点过头了,它会为您提供一些东西,让您可以解析带有方括号和圆括号、单引号和双引号的语句,如您所愿嵌套。 例如,您可以解析这样的内容(用 Common Lisp 编写的示例):
您可以使用嵌套、方括号(方)和圆括号(圆)、单引号和双引号字符串,并且它的可扩展性非常好。
这个想法基本上是有限状态机的可配置实现,它逐个字符地构建抽象语法树。 我建议您查看源代码(请参阅上面的链接),以便您了解如何执行此操作。 它可以通过正则表达式来实现,但是尝试使用 RE 编写系统,然后尝试扩展它(甚至理解它)。
Well, I've encountered this problem quite a few times, which led me to write my own system for parsing any kind of syntax.
The result of this can be found here; note that this may be overkill, and it will provide you with something that lets you parse statements with both brackets and parentheses, single and double quotes, as nested as you want. For example, you could parse something like this (example written in Common Lisp):
You can use nesting, brackets (square) and parentheses (round), single- and double-quoted strings, and it's very extensible.
The idea is basically a configurable implementation of a Finite State Machine which builds up an abstract syntax tree character-by-character. I recommend you look at the source code (see link above), so that you can get an idea of how to do it. It's capable via regular expressions, but try writing a system using REs and then trying to extend it (or even understand it) later.
仅适用于报价。
Works for quotes only.