Split string based on a regular expression

后端 未结 4 1763
我寻月下人不归
我寻月下人不归 2020-11-27 12:26

I have the output of a command in tabular form. I\'m parsing this output from a result file and storing it in a string. Each element in one row is separated by one or more w

相关标签:
4条回答
  • 2020-11-27 13:02

    By using (,), you are capturing the group, if you simply remove them you will not have this problem.

    >>> str1 = "a    b     c      d"
    >>> re.split(" +", str1)
    ['a', 'b', 'c', 'd']
    

    However there is no need for regex, str.split without any delimiter specified will split this by whitespace for you. This would be the best way in this case.

    >>> str1.split()
    ['a', 'b', 'c', 'd']
    

    If you really wanted regex you can use this ('\s' represents whitespace and it's clearer):

    >>> re.split("\s+", str1)
    ['a', 'b', 'c', 'd']
    

    or you can find all non-whitespace characters

    >>> re.findall(r'\S+',str1)
    ['a', 'b', 'c', 'd']
    
    0 讨论(0)
  • 2020-11-27 13:05

    The str.split method will automatically remove all white space between items:

    >>> str1 = "a    b     c      d"
    >>> str1.split()
    ['a', 'b', 'c', 'd']
    

    Docs are here: http://docs.python.org/library/stdtypes.html#str.split

    0 讨论(0)
  • 2020-11-27 13:09

    Its very simple actually. Try this:

    str1="a    b     c      d"
    splitStr1 = str1.split()
    print splitStr1
    
    0 讨论(0)
  • 2020-11-27 13:12

    When you use re.split and the split pattern contains capturing groups, the groups are retained in the output. If you don't want this, use a non-capturing group instead.

    0 讨论(0)
提交回复
热议问题