从python中的正则表达式中拆分字符串

时间:2013-03-21 15:42:23

标签: python regex

我有这样的模式:

" 1+2;\r\n\r(%o2) 3\r\n(%i3) "

我想将它们分成:

[" 1+2;","(%o2) 3","(%i3)"]

第一个模式的正则表达式很难构造,因为它可能是用户要求代数系统的任何东西,第二个可能是:

'\(%o\d+\).'

以及最后这样的事情:

'\(%i\d+\)

我不是严格地受到正则表达式部分的困扰,但是一旦我知道正确的模式,如何实际拆分。 我怎么会分开这个?

2 个答案:

答案 0 :(得分:1)

如何分割(\r|\n)+

答案 1 :(得分:1)

此代码是否适合您?

patterns = [p.strip() for x in " 1+2;\r\n\r(%o2) 3\r\n(%i3) ".split("\r\n")]

澄清:

>>> patterns = " 1+2;\r\n\r(%o2) 3\r\n(%i3) ".split("\r\n")
>>> patterns
[' 1+2;', '\r(%o2) 3', '(%i3) ']
>>> patterns = [p.strip() for p in patterns]
['1+2;', '(%o2) 3', '(%i3)']

这样你就可以分割线条并摆脱不必要的白色字符。

编辑:另外:Python String还有splitlines()方法:

splitlines(...)
    S.splitlines([keepends]) -> list of strings

    Return a list of the lines in S, breaking at line boundaries.
    Line breaks are not included in the resulting list unless keepends
    is given and true.

因此,此代码可能会更改为:

patterns = [p.strip() for x in " 1+2;\r\n\r(%o2) 3\r\n(%i3) ".splitlines()]

这可能会解决NL没有CR和所有不同组合的问题。