我正在使用许多不同的正则实现实现,因为这发生在多个系统(Linux,Windows,VS,Notepad 等(上;这是我有一个想要删除自动尺寸的客户的地方。目的是使用以下方面的工具来找到任何具有宽度但没有自动化的行,然后添加自动缩合。我只是在问如何找到它,,但我打算将我在替换字符串中找到的东西用于给定的编辑器。我有替代品的位置:我只是没有弄清楚当另一个远离一个时,如何与另一个相处。
使用https://regex101.com/我尝试了数十个搜索字符串。
这是我的搜索字符串的起点,也是几次尝试将自动化排除在生产线上任何地方的尝试。字符串2和3基本上是同一件事,但是我不知道该尝试什么。我认为任何适合LookBehind的东西都适用于LookAhead,但是如您所见,我什至无法工作。
(?<! AutoWidth="false") width="d*"(?! AutoWidth="false") (?<! AutoWidth="false").*? width="d*"(?! AutoWidth="false") (?<! AutoWidth="false")[0-9a-zA-Z" =]*? width="d*"(?! AutoWidth="false")
我被卡住了,在自动化和宽度之间距离我避开了我。
这些是我的目标
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和amp上可用。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>
这与没有兄弟姐妹AutoWidth
的width
属性匹配,将其复制并添加缺失的兄弟姐妹。在这里,我仅将范围限制为column
元素,但是您可以通过:
<xsl:template match="@width[not(../@AutoWidth)]">
xpath就是这样: //column[@width and not(@AutoWidth)]
。
说明:
-
//column
找到所有<column ...>
元素 -
[...]
包含prepticates -
@width
检查@widht
属性的存在 -
not(@AutoWidth)
检查@AutoWidth
属性的缺失。
我在freeformatter.com上使用XPath测试仪进行测试。
我添加了一个<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"/>
。我相信这就是您所需要的。
grep还有另一个快速解决方案。它需要一个bash壳,例如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"/>