Regular Expression Searching Matching One Word but Not Another

天涯浪子 提交于 2019-12-24 06:42:04

问题


<ReportExport ID="export1" runat="server" AlertNoTests="false" PDFPageOrientation="Portrait"
            HideExcel="true" OnPDFClicked="CreatePDF" AllowPDFOptions="true" HideBulkPDFOptions="false"
            HideOrientation="true" HidePaperSize="true" MaxReportsAtOnce="250" HideTextExport="true" />

I'm trying to use Visual Studio's find feature using regular expressions to find ReportExport in my entire solution where the HideTextExport property is not being set. This is only ever defined in the markup once on a given page.

Any ideas on how I would find where ReportExport exists... but HideTextExport does not exist in the text?

Thanks in advance!


回答1:


This works for me:

\<ReportExport(:Wh+~(HideTextExport):w=:q)+:Wh*/\>

:Wh+ matches the whitespace preceding the attribute name and :w matches the name, but only after ~(HideTextExport) confirms that the name is not "HideTextExport". :q matches the attribute's value (assuming values are always quoted). < and > have to be escaped or VS Find will treat them as word boundaries.

This is effectively the same as the .NET regex,

<ReportExport(?:\s+(?!HideTextExport)[A-Za-z]+="[^"]+")+\s*/>



回答2:


First off one should install the Productivity Power tools to Visual Studio (via Tools->Extension Manager) and use .net regex instead of the antiquated regex provided out of the box for the Visual Studio Find.

With that the user could use this regex pattern (if the productivity power tools has singleline turned on to handle the span of lines for the element):

(ReportExport.+?HideTextExport="false")

That will return all reportexports where its false and one could tweak the regex to change it to replace false to true.

But...if the HideTextExport is missing, this makes regex a poor choice to use to find this element because the uncertantity of the location of the attribute makes the .* or .+ too greedy and ends reporting false positives when trying to find a missing text in a match.

A generalized way of saying that is, regex finds patterns and that is its job, but it requires lexical analsys to find missing patterns where regex simply cannot.



来源:https://stackoverflow.com/questions/9912914/regular-expression-searching-matching-one-word-but-not-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!