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

标签 regex xslt language-agnostic regex-lookarounds

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

使用 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>

最佳答案

这在 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>

将返回:
<?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>

这匹配 width没有同级的属性 AutoWidth , 复制它并添加缺少的兄弟。这里我将范围限制为 column仅元素,但您可以通过执行以下操作将其扩展到任何元素:
<xsl:template match="@width[not(../@AutoWidth)]">

关于匹配字符串的正则表达式,但前提是同一行上的任何地方都不存在另一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522731/

相关文章:

php - 基本正则表达式 PHP 问题

java - 日期的正则表达式省略月份部分的一个字符

jquery - 如何用 th 标签替换所有 tr 标签

xml - XSLT 如何在模板内的元素中添加命名空间前缀

xml - 我可以使用 XSLT 1.0 在 XML 元素的文本内容周围添加 <p></p> 标签吗?

oop - 理解封装和抽象的简单方法

Javascript 在 20 个字符后分割

javascript - 如何使用一个 XSLT 文件中的变量到另一个 XSLT 文件

language-agnostic - 对于内存有限的计算机,停止真的无法确定吗?

math - float 学坏了吗?