regex Python正则表达式忽略第一个出现的项

92vpleto  于 2023-10-22  发布在  Python
关注(0)|答案(3)|浏览(139)

尝试忽略第一次和第三次匹配并替换所有日期信息。这是一条不起作用的小径。

Input = '''
Captured on: 08-29-2023 09:43:26
Start Time: 08-28-2023 11:11:57
1St Cycle Time: 08-28-2023 11:12:06 

Channel-00 Cycles Completed: 4404 
Channel-00 Triggered Time: 08-29-2023 00:45:08 
Channel-01 Cycles Completed: 24 
Channel-01 Triggered Time: 08-28-2023 11:15:57'''

stra = re.sub("^(?!Captured.*$).*?(\d+-\d+-2023 )", "", Input)

预期输出:

Captured on: 08-29-2023 09:43:26
Start Time: 11:11:57
1St Cycle Time: 08-28-2023 11:12:06 

Channel-00 Cycles Completed: 4404 
Channel-00 Triggered Time: 00:45:08 
Channel-01 Cycles Completed: 24 
Channel-01 Triggered Time: 11:15:57
lnvxswe2

lnvxswe21#

print(re.sub(r'(Start Time:|Triggered Time:)\s+[0-9-]+(\s[0-9:]+)',r'\1\2', Input))

Captured on: 08-29-2023 09:43:26
Start Time: 11:11:57
1St Cycle Time: 08-28-2023 11:12:06 

Channel-00 Cycles Completed: 4404 
Channel-00 Triggered Time: 00:45:08 
Channel-01 Cycles Completed: 24 
Channel-01 Triggered Time: 11:15:57
0qx6xfy6

0qx6xfy62#

假设你知道你想忽略的行的开始部分,你可以使用这个正则表达式进行匹配:

^((?:Captured|1St) .*)|\b\d\d-\d\d-2023\s*

并替换为:

\1

RegEx Demo

RegEx详情:

  • ^:开始
  • ((?:Captured|1St) .*):匹配线以Captured1st开头,后跟一个空格和所有内容,直到结束
  • |:或
  • \b\d\d-\d\d-2023\s*:匹配日期部分
5vf7fwbs

5vf7fwbs3#

我假设目标是删除格式为dd-mm-yyyy的日期字符串,当该字符串为:

  • 不在字符串的第一行
  • 后跟一个或多个空格,后跟24小时格式的时间(例如,17:35:16),后跟零个或多个空格,然后是行结束符(\n,对于Windows,\r\n
  • 后面不跟空行

这并不是说我没有对要删除的日期字符串之前的文本做任何假设,以便在将来更改该文本时不需要更改正则表达式。
在上述假设下,匹配以下正则表达式的文本可以用捕获组1的内容替换。

(?<!\A)^(.*) \d{2}-\d{2}-\d{4}(?= +\d{2}:\d{2}:\d{2} *\r?\n(?! *\r?\n))

Demo
正则表达式可以分解如下。

(?<!\A)             # negative lookbehind asserts current string position
                    # is not at the beginning of the string
^                   # match the beginning of a line
(.*)                # match zero or more characters other than line terminators,
                    # as many as possible, and save to capture group 1
[ ]                 # match a space
\d{2}-\d{2}-\d{4}   # match the string representing the date
(?=                 # begin a positive lookahead
  [ ]+              #   match one or more spaces
  \d{2}:\d{2}:\d{2} #   match the string representing the time
  [ ]*\r?\n         #   match zero or more spaces followed by a line terminator
  (?! *\r?\n)       #   negative lookahead asserts an empty line does not follow
)                   # end the positive lookahead

在上面的例子中,我在一个字符类([ ])中包含了大部分空格,只是为了让它们可见。

相关问题