正则表达式匹配一个字符串,但前提是另一个字符串不在同一行的任何地方

Regex that matches a string but only if another string is not present anywhere on the same line

我正在使用许多不同的 Regex 实现,因为这发生在多个系统上(Linux、Windows、VS、notepad++ 等);这就是我有一个客户想要删除自动调整大小的地方。目的是在其中使用正则表达式工具来查找任何具有宽度但没有自动宽度的行,然后 add 自动宽度。我只是想问一下如何找到它,但我打算使用我在此处找到的内容作为给定编辑器的替换字符串。 我已经找到了替换位:我只是没有'当另一个离那个很远的时候,我不知道如何用一个去掉另一个。

使用 https://regex101.com/ 我已经尝试了几十个搜索字符串。

这是我搜索字符串的起点,并进行了几次尝试来获得环视以在该行的任何位置排除 AutoWidth。字符串 2 和 3 基本上是同一回事,但我不知道还能尝试什么。我假设任何适用于后视的东西也适用于前视,但正如你所看到的,我什至不能在后视工作。

(?<! AutoWidth="false") width="\d*"(?! AutoWidth="false")
(?<! AutoWidth="false").*? width="\d*"(?! AutoWidth="false")
(?<! AutoWidth="false")[0-9a-zA-Z" =]*? width="\d*"(?! AutoWidth="false")

我被卡住了,AutoWidth 和 width 之间的距离让我难以理解。

这些是我的目标

1->  <column name="Selected" AutoWidth="false" IsEditable="true" datatype="bool" width="20"/>
2->  <column width="40" AutoWidth="false" name="ExternalIdOrEmpty" index="XIDname" sort="true"/>
3->  <column width="40" name="Tax Rate" index="TRname" sort="true" AutoWidth="false"/>
4->  <column width="40" name="Total Tax" index="TTname" sort="true"/>
5->  <column name="Tax Deductible" index="TDname" sort="true"/>

我想找到包含

的所有行

width="\d*"

但不包含

AutoWidth="\d*"

在同一行的任意位置。

这意味着上面示例中只有第 4 行符合我的标准。

更新:

我愿意使用任何其他工具来完成工作。所以 XSLT 等都很好。唯一的要求是该工具通常在 Windows、Linux 和 Mac 上可用,并且是开源或免费的,并且也是众所周知的。

完整 xml 很大。此处的编辑功能限制为 30,00 个字符,但这是一个很好的示例。

<?xml version="1.0" encoding="utf-8" ?>
<spread>
  <ViewPatientOutboundReferralFilter>
    <FindColumn name="ViewUid" index="guid" visible="false" />
    <FindColumn name="Selected" caption=" " visible="true" IsEditable="true" datatype="bool"/>
    <FindColumn name="PatientName" caption="Patient Name" visible="true" width="150" hyperlink="true" AutoWidth="false"/>
    <FindColumn name="ReferToProviderName" caption="Provider" visible="true" AutoWidth="false" width="150" hyperlink="true"/>
    <FindColumn name="ReferredToMedicalServicesProviderName" caption="Medical Services Provider" visible="true" width="150" hyperlink="true"/>
    <FindColumn name="ProviderRole" caption="Provider Role" visible="true" width="80" hyperlink="true"/>
    <FindColumn name="StatusName" caption="Current Status" visible="true" width="100" hyperlink="true"/>
    <FindColumn name="ServiceSiteName" caption="Service Site" visible="true"/>
    <FindColumn name="VisitDate" caption="Visit Date" visible="true" width="90" datatype="date"/>
    <FindColumn name="AppointmentDate" caption="Appointment Date" visible="true" datatype="datetime" width="90"/>
    <FindColumn name="Notes" caption="Comments" visible="true" width="120"/>
    <FindColumn name="AppointmentNotes" caption="Referral Notes" visible="true" width="120"/>
    <FindColumn name="DisplayName" visible="false" index="name"  />
    <FindColumn name="ProviderUid" visible="false" storeproperty="true" />
    <FindColumn name="VisitUid" visible="false" storeproperty="true" />
    <FindColumn name="CreatedDate" caption="Created Date" visible="true" datatype="date" width="90"/>
    <FindColumn name="RequestingName" caption="Requesting Provider" visible="true" width="150" />
  </ViewPatientOutboundReferralFilter>
  <FeeScheduleFeeAA rowcount="3">
    <column row="0" rowspan="3" caption="Code" width="50" name="Procedure.Code" sort="true" index="name" />
    <column row="0" rowspan="3" caption="Description" relwidth="100%" width="80" AutoWidth="false" name="Procedure.ShortDescription" sort="true" />
    <column row="0" rowspan="3" caption="Amount Allowed" width="60" AutoWidth="false" name="Fee" IsEditable="true" datatype="currency" />
    <column row="0" rowspan="3" caption="Global Period" width="40" AutoWidth="false" name="GlobalPeriodDays" IsEditable="true" datatype="number" decimalPlaces="0" minValue="0" maxValue="1000" />
    <column row="0" colspan="5" caption="Coinsurance" />
    <column row="1" colspan="2" caption="Insurance Percent" />
    <column row="2" caption=" " width="30" AutoWidth="false" name="RadioInsurancePercent" IsEditable="true" datatype="radio" radioOrientation="vertical" radioItems=" " />
    <column row="2" caption="Value" width="70" AutoWidth="false" name="InsurancePercent" IsEditable="true" datatype="number" decimalPlaces="0" minValue="0" maxValue="100" />
    <column row="1" colspan="2" caption="Insurance Plan" />
    <column row="2" caption="PCP/Specialist" width="95" AutoWidth="false" name="RadioInsurancePlanPhysician" IsEditable="true" datatype="radio" radioOrientation="vertical" radioItems=" " />
    <column row="2" caption="Other" width="55" AutoWidth="false" name="RadioInsurancePlanOther" IsEditable="true" datatype="radio" radioOrientation="vertical" radioItems=" " />
    <column row="1" rowspan="2" caption="Copay Amount" width="70" AutoWidth="false" name="FixedCopayAmount" datatype="currency" IsEditable="true" />
    <column row="0" rowspan="3" caption="Contract Type" width="55" AutoWidth="false" name="ContractTypeCode.Name" sort="true"/>
    <column row="0" rowspan="3" caption="Family Planning" width="55" AutoWidth="false" name="FamilyPlanning" IsEditable="true" datatype="bool" />
    <column row="0" rowspan="3" caption="Alt Insurance Plan" width="55" AutoWidth="false" name="UseAlternateInsurancePlan" IsEditable="true" datatype="bool" />
    <column row="0" rowspan="3" caption="Edit Billing Rule" width="70" visible="false" IsEditable="true" datatype="CustomCellType" celltype="iMedica.Prm.Client.UI.BaseControls.Spread.PrmNeoCellImageButton,iMedica.Prm.Client.UI.BaseControls" ShowSortIndicator="false" ImageResourceName="iMedica.Prm.Client.UI.BaseControls.Icons.BillingRule.png" ImageResourceAssembly="iMedica.Prm.Client.UI.BaseControls" sort="false" />
  </FeeScheduleFeeAA>
</spread>

xpath 是这样的://column[@width and not(@AutoWidth)].

解释:

  • //column 找到所有 <column ...> 个元素
  • [...] 包含谓词
  • @width 检查是否存在 @widht 属性
  • not(@AutoWidth) 检查缺少 @AutoWidth 属性。

我在 freeformatter.com 上使用 xpath tester 进行测试。

我添加了一个 <foo> 元素以使其格式正确 XML。 IE。这是我用于测试的实际 xml:

<foo>
  <column name="Selected" AutoWidth="false" IsEditable="true" datatype="bool" width="20"/>
  <column width="40" AutoWidth="false" name="ExternalIdOrEmpty" index="XIDname" sort="true"/>
  <column width="40" name="Tax Rate" index="TRname" sort="true" AutoWidth="false"/>
  <column width="40" name="Total Tax" index="TTname" sort="true"/>
  <column name="Tax Deductible" index="TDname" sort="true"/>
</foo>

那么,这是 xpath://column[@width and not(@AutoWidth)]

它只选择了一项:<column index="TTname" name="Total Tax" sort="true" width="40"/>。我相信这就是你需要的。

这是 XSLT 中的一个相当微不足道的问题。给定格式正确的输入,例如:

XML

<root>
    <column name="Selected" AutoWidth="false" IsEditable="true" datatype="bool" width="20"/>
    <column width="40" AutoWidth="false" name="ExternalIdOrEmpty" index="XIDname" sort="true"/>
    <column width="40" name="Tax Rate" index="TRname" sort="true" AutoWidth="false"/>
    <column width="40" name="Total Tax" index="TTname" sort="true"/>
    <column name="Tax Deductible" index="TDname" sort="true"/>
</root>

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="column/@width[not(../@AutoWidth)]">
    <xsl:copy/>
    <xsl:attribute name="AutoWidth">False</xsl:attribute>
</xsl:template>

</xsl:stylesheet>

将return:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <column name="Selected" AutoWidth="false" IsEditable="true" datatype="bool" width="20"/>
  <column width="40" AutoWidth="false" name="ExternalIdOrEmpty" index="XIDname" sort="true"/>
  <column width="40" name="Tax Rate" index="TRname" sort="true" AutoWidth="false"/>
  <column width="40" AutoWidth="False" name="Total Tax" index="TTname" sort="true"/>
  <column name="Tax Deductible" index="TDname" sort="true"/>
</root>

这匹配没有兄弟 AutoWidthwidth 属性,复制它并添加缺少的兄弟。在这里,我将范围限制为仅 column 元素,但您可以通过执行以下操作将其扩展到 any 元素:

<xsl:template match="@width[not(../@AutoWidth)]">

还有一个使用 grep 的快速解决方案。它需要一个 bash shell,例如来自 windows.

git-bash 之一
cat lines.txt | grep -P -v 'AutoWidth="[^"]*"' | grep -P 'width="[^"]*"'

解释:

  • cat lines.txt - 这是您的数据来源
  • grep -P' 为简单起见启用 perl 语法
  • grep -v 只保留不匹配的行
  • "[^"]*" 匹配引号之间的所有内容,但在第一个引号后不再继续

这是您的示例数据的结果:

4->  <column width="40" name="Total Tax" index="TTname" sort="true"/>