【问题标题】:Split line by spaces, but preserve strings within backticks用空格分割行,但保留反引号内的字符串
【发布时间】:2019-07-13 21:56:53
【问题描述】:

我有以下格式化数据:

testing 25 `this is a test`
hello `world hello world`
log "log1" "log2" `third log`

我目前正在使用正则表达式和shlex的组合,但我遇到了问题,如上所示

import re, shlex

def tokenize(line):
    graveKeyPattern = re.compile(r'^ *(.*) (`.*`) *')
    if '`' in line:
        tokens = re.split(graveKeyPattern, line)
        tokens = tokens[1:3]
    else:
        tokens = shlex.split(line)
    #end if/else
    print(tokens)
    return tokens
#end tokenize

lines = []
lines.append('testing 25 `this is a test`')
lines.append('hello `world hello world`')
lines.append('log "log1" "log2" `third log`')
lines.append('testing2 "testing2 in quotes" 5')

for line in lines:
    tokenize(line)

这是我得到的输出:

['testing 25', '`this is a test`']
['hello', '`world hello world`']
['log "log1" "log2"', '`third log`']
['testing2', 'testing2', 'in', 'quotes', '5']

这是我需要的输出:

['testing', '25', '`this is a test`']
['hello', '`world hello world`']
['log', 'log1', 'log2', '`third log`']
['testing2', 'testing2 in quotes', '5']

【问题讨论】:

  • 试试["{}{}{}{}".format(x.group(1),x.group(2),x.group(3),x.group(4)) for x in re.finditer(r'''`([^`]*)`|"([^"]*)"|'([^']*)'|(\S+)''', line)]["{}{}{}{}".format(a,b,c,d) for a,b,c,d in re.findall(r'''`([^`]*)`|"([^"]*)"|'([^']*)'|(\S+)''', line)]
  • 啊,"`([^`]*)`" 实际上必须是 (`[^`]*`),因为反引号应该在里面。

标签: python regex python-3.x lexical-analysis


【解决方案1】:

有时,匹配你想要的比分割你不想要的更容易。

通过匹配反引号或非空格/引号之间的内容,这适用于您的测试:

lines = []
lines.append('testing 25 `this is a test`')
lines.append('`world hello world` hello ')
lines.append('log "log1" "log2" `third log` log3')

import re
[re.findall(r'((?:`.*?`)|[^\"\s]+)', s) for s in lines]

结果

[['testing', '25', '`this is a test`'],
 ['`world hello world`', 'hello'],
 ['log', 'log1', 'log2', '`third log`', 'log3']]

【讨论】:

  • 这很好用,谢谢!你能告诉我一切都意味着什么吗?我不太懂正则表达式。
  • @wolfy 有两个主要的东西被| 分开,表示一个或另一个:`` .*? `` 匹配反引号和中间的任何东西,[^\"\s]+ 是一个或多个东西这不是空格 (\s) 或引号。 ^[..] 中表示 not。一切都在(..) 中,表示一个捕获组,但以?: 开头的括号表示一个非捕获组,因为我们不想捕获它两次。
猜你喜欢
  • 2013-12-13
  • 2015-09-03
  • 2018-10-23
  • 2012-08-03
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多