English 中文(简体)
Regex to match part of string, when match does not contain a specific string - PCRE grep
原标题:

I m using TextWrangler grep to perform find/replace on multiple files and have run into a wall with the last find/replace I need to perform. I need to match any text between "> and the first instance of a <br /> in a line but the match cannot contain the character sequence [xcol]. The regex flavor is Perl-Compatible (PCRE) so lookbehind needs to be fixed-length.

Example Text to Search:

<p class="x03">FooBar<br />Bar</p>
<p class="x03">FooBar [xcol]<br />Bar</p>
<p class="x06">Hello World<br />[xcol]foo[xcol]bar<br /></p>
<p class="x07">Hello World[xcol]<br />[xcol]foo[xcol]bar<br /></p>  

Desired behavior of regex:
1st Line match ">FooBar<br />
2nd Line no match
3rd Line match ">Hello World<br />
4th Line no match

The text between "> and the <br /> will be captured in a group to be used with the replace function. The closest I got was using the following regex with negative lookahead, but this will not match the 3rd line as desired:

">((?!.*?[xcol]).*?)<br />

Any help or advice is appreciated. Thank you.

最佳回答

Try this regex:

">((?![xcol]).)*<brs*/>

A (short) explanation:

">               # match  "> 
(                # start group 1
  (?![xcol]).   #   if  [xcol]  can t be seen ahead, match any character (except line breaks)
)                # end group 1
*                # repeat group 1 zero or more times
<brs*/>         # match  <br /> 

If you need to match line breaks for . as well, either enable DOT-ALL (add (?s) before the .) or replace the . with something like [sS]

问题回答

暂无回答




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签