草庐IT

Python 正则表达式引擎 - "look-behind requires fixed-width pattern"错误

coder 2023-05-24 原文

我正在尝试处理 CSV 格式的字符串中不匹配的双引号。

准确地说,

"It "does "not "make "sense", Well, "Does "it"

应该改正为

"It" "does" "not" "make" "sense", Well, "Does" "it"

所以基本上我想做的是

replace all the ' " '

  1. Not preceded by a beginning of line or a comma (and)
  2. Not followed by a comma or an end of line

with ' " " '

为此,我使用以下正则表达式

(?<!^|,)"(?!,|$)

问题是当 Ruby 正则表达式引擎 ( http://www.rubular.com/ ) 能够解析正则表达式时,python 正则表达式引擎 ( https://pythex.org/ , http://www.pyregex.com/ ) 会抛出以下错误

Invalid regular expression: look-behind requires fixed-width pattern

而使用 python 2.7.3 它会抛出

sre_constants.error: look-behind requires fixed-width pattern

谁能告诉我这里的python有什么烦恼?


编辑:

根据 Tim 的回复,我得到了以下多行字符串的输出

>>> str = """ "It "does "not "make "sense", Well, "Does "it"
... "It "does "not "make "sense", Well, "Does "it"
... "It "does "not "make "sense", Well, "Does "it"
... "It "does "not "make "sense", Well, "Does "it" """
>>> re.sub(r'\b\s*"(?!,|$)', '" "', str)
' "It" "does" "not" "make" "sense", Well, "Does" "it" "\n"It" "does" "not" "make" "sense", Well, "Does" "it" "\n"It" "does" "not" "make" "sense", Well, "Does" "it" "\n"It" "does" "not" "make" "sense", Well, "Does" "it" " '

在每一行的末尾,在 'it' 旁边添加了两个双引号。

所以我对正则表达式做了一个很小的改动来处理换行符。

re.sub(r'\b\s*"(?!,|$)', '" "', str,flags=re.MULTILINE)

但这给出了输出

>>> re.sub(r'\b\s*"(?!,|$)', '" "', str,flags=re.MULTILINE)
' "It" "does" "not" "make" "sense", Well, "Does" "it"\n... "It" "does" "not" "make" "sense", Well, "Does" "it"\n... "It" "does" "not" "make" "sense", Well, "Does" "it"\n... "It" "does" "not" "make" "sense", Well, "Does" "it" " '

最后一个 'it' 单独有两个双引号。

但我想知道为什么'$'行尾字符不能识别行已结束。


最终的答案是

re.sub(r'\b\s*"(?!,|[ \t]*$)', '" "', str,flags=re.MULTILINE)

最佳答案

Python re后视确实需要固定宽度,当您在后视模式中有不同长度的交替时,有几种方法可以处理这种情况:

  • 重写模式,这样您就不必使用交替(例如,蒂姆的上述答案使用单词边界,或者您也可以使用与当前模式完全等效的 (?<=[^,])"(?!,|$),在双引号,或匹配用空格括起来的单词的常用模式,(?<=\s|^)\w+(?=\s|$),可以写成 (?<!\S)\w+(?!\S)),或者
  • 拆分回顾:
    • 需要在一组中交替使用正向后视(例如 (?<=a|bc) 应重写为 (?:(?<=a)|(?<=bc)))
    • 如果lookbehind 中的模式是 anchor 与单个字符的交替,您可以反转lookbehind 的符号并使用内部带有char 的否定字符类。例如。 (?<=\s|^)匹配空格或字符串/行的开头(如果使用 re.M)。所以,在 Python re , 使用 (?<!\S) . (?<=^|;)将转换为 (?<![^;]) .如果您还想确保行首也匹配,请添加 \n到否定字符类,例如(?<![^;\n]) (见 Python Regex: Match start of line, or semi-colon, or start of string, none capturing group)。注意 (?<!\S) 不需要这样做。作为 \S与换行符不匹配。
    • 可以将否定的lookbehinds 串联起来(例如,(?<!^|,)"(?!,|$) 应该看起来像 (?<!^)(?<!,)"(?!,|$))。

或者,只需安装 PyPi regex module使用 pip install regex (或 pip3 install regex )并享受无限宽度的向后看。

关于Python 正则表达式引擎 - "look-behind requires fixed-width pattern"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20089922/

有关Python 正则表达式引擎 - "look-behind requires fixed-width pattern"错误的更多相关文章

  1. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  6. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  9. ruby - RVM "ERROR: Unable to checkout branch ."单用户 - 2

    我在新的Debian6VirtualBoxVM上安装RVM时遇到问题。我已经安装了所有需要的包并使用下载了安装脚本(curl-shttps://rvm.beginrescueend.com/install/rvm)>rvm,但以单个用户身份运行时bashrvm我收到以下错误消息:ERROR:Unabletocheckoutbranch.安装在这里停止,并且(据我所知)没有安装RVM的任何文件。如果我以root身份运行脚本(对于多用户安装),我会收到另一条消息:Successfullycheckedoutbranch''安装程序继续并指示成功,但未添加.rvm目录,甚至在修改我的.bas

  10. ruby - 如何关闭 ruby​​ gem "Spreadsheet?"中的文件 - 2

    下面的代码在我第一次运行它时就可以正常工作:require'rubygems'require'spreadsheet'book=Spreadsheet.open'/Users/me/myruby/Mywks.xls'sheet=book.worksheet0row=sheet.row(1)putsrow[1]book.write'/Users/me/myruby/Mywks.xls'当我再次运行它时,我会收到更多消息,例如:/Library/Ruby/Gems/1.8/gems/spreadsheet-0.6.5.9/lib/spreadsheet/excel/reader.rb:11

随机推荐