Regex 与字符串匹配的正则表达式,但仅当同一行中的任何位置都不存在另一个字符串时

Regex 与字符串匹配的正则表达式,但仅当同一行中的任何位置都不存在另一个字符串时,regex,xslt,language-agnostic,regex-lookarounds,Regex,Xslt,Language Agnostic,Regex Lookarounds,我正在使用许多不同的正则表达式实现,因为这在几个系统(Linux、Windows、VS、notepad++等)上都会发生;这正是我的一位客户想要取消自动上浆的地方。其目的是使用regex工具查找任何有宽度但没有自动宽度的行,然后添加自动宽度。我只是想问一下如何找到它,但我打算用我在这里找到的替换字符串来替换我给定的编辑器。我把替换的部分记下来了:我只是还没有弄清楚,当另一个离另一个很远的时候,如何把另一个拿出来 使用我已经尝试了几十个搜索字符串 这是我搜索字符串的起点,我尝试了几次让lookar

我正在使用许多不同的正则表达式实现,因为这在几个系统(Linux、Windows、VS、notepad++等)上都会发生;这正是我的一位客户想要取消自动上浆的地方。其目的是使用regex工具查找任何有宽度但没有自动宽度的行,然后添加自动宽度。我只是想问一下如何找到它,但我打算用我在这里找到的替换字符串来替换我给定的编辑器。我把替换的部分记下来了:我只是还没有弄清楚,当另一个离另一个很远的时候,如何把另一个拿出来

使用我已经尝试了几十个搜索字符串

这是我搜索字符串的起点,我尝试了几次让lookarounds排除行中任何位置的AutoWidth。字符串2和3基本上是一样的,但我不知道还能尝试什么。我假设任何对“后向”有效的东西都会对“前向”有效,但正如你所看到的,我甚至不能让“后向”有效


(?xpath是这样的:
//列[@width而非(@AutoWidth)]

说明:

  • //列
    查找所有
    元素
  • […]
    包含谓词
  • @width
    检查是否存在
    @widt
    属性
  • not(@AutoWidth)
    检查是否缺少
    @AutoWidth
    属性
我使用freeformatter.com上的

我添加了一个
元素,使其成为格式良好的XML。也就是说,这是我用于测试的实际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:
//列[@width和not(@AutoWidth)]


它只选择一项:
。我相信这就是您所需要的。

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

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

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"/>
4->

学习如何使用html解析器,你的日子会更快乐。对于python:
beautifulsoup
,对于java:
jsoup
对于php:
DOMDocument
等等…@PedroLobito你喜欢什么类型的html解析器?它能帮我解决XML问题吗?xpath很容易。不过正则表达式解决方案看起来很难。请e更新您的问题并发布一个更完整的输入代码和所需输出的示例。这是
xml
还是
html
?强制性参考:。这是一个很好的起点。有没有方法在应用修复的情况下发布整个文档?我使用regex的原因是因为我也可以使用替换方面,而且还有通过修复文档。我想我不清楚这一点。@Dysmondad这正是它的作用:所有与第二个模板不匹配的节点都由身份转换模板处理-即按原样复制。谢谢。通过XML插件->转换XML与Notepad++一起工作。这正是我需要的。
<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>
<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>
<xsl:template match="@width[not(../@AutoWidth)]">
cat lines.txt | grep -P -v 'AutoWidth="[^"]*"' | grep -P 'width="[^"]*"'
4->  <column width="40" name="Total Tax" index="TTname" sort="true"/>