Python正则表达式spli

2024-04-26 17:50:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字段可以过滤一些db列。但我无法按需要拆分搜索字符串:

我举了一个例子:

import re
search = '   test "no splits234" this-splits   this_not_splits  asdf123  '
re.split(r'[\s]*[\W][\s]*', search.strip())
['test', 'no', 'splits234', 'this', 'splits', 'this_not_splits', 'asdf123']

需要此输出:

['test', 'no splits234', 'this', 'splits', 'this_not_splits', 'asdf', '123']

不要拆分引号中的内容,也不要拆分数字中的文本。我该怎么做


Tags: no字符串testimportredbsearchnot
1条回答
网友
1楼 · 发布于 2024-04-26 17:50:01

可以将findall与此正则表达式一起使用:

>>> search = '   test "no splits234" this-splits   this_not_splits  asdf123  '
>>> print re.findall(r'"[^"\\]*(?:\\.[^"\\]*)*"|[^\s-]+', search)
['test', '"no splits234"', 'this', 'splits', 'this_not_splits', 'asdf123']

正则表达式详细信息:

  • 表达式"[^"\\]*(?:\\.[^"\\]*)*"匹配由双引号括起的字符串,忽略所有转义引号
  • 如果没有带引号的字符串,那么我们只需使用[^\s-]+匹配1+非空格、非连字符

如果要避免捕获引号,请使用:

>>> print re.findall(r'(?<=")[^"\\]*(?:\\.[^"\\]*)*(?=")|[^\s"-]+', search)
['test', 'no splits234', 'this', 'splits', 'this_not_splits', 'asdf123']

更新:

OP还显示最后的asdf123分裂成asdf123。为此,以下正则表达式可能起作用:

>>> print re.findall(r'(?<=")[^"\\]*(?:\\.[^"\\]*)*(?=")|\b[a-zA-Z]+(?=\d)|(?<=[a-zA-Z])\d+|[^\s"-]+', search)
['test', 'no splits234', 'this', 'splits', 'this_not_splits', 'asdf', '123']

相关问题 更多 >