Syntax for LookBehind regex expression

时间:2017-12-18 05:40:54

标签: regex

I need a regex expression which will match 'xmlAttributeReplace=' and the quote following the curly bracket. I then replace these matches with an empty string.

Sample input is

<autoUnlockSection xmlAttributeReplace="#{token.variable.sample}" 
     xdt:Transform="Replace"/>

I currently have the following

xmlAttributeReplace="|(?<=})"

My problem is how I'm finding the quote following the curly bracket is incorrect. The look behind should be looking for

  1. xmlAttributeReplace=
  2. no spaces between the quote and xmlAttributeReplace=

Is there a guru here at regex who might know the correct syntax?

1 个答案:

答案 0 :(得分:1)

If you want to remove the attribute and the value, you should use:

xmlAttributeReplace="[^" ]+"

[^" ]+ simple mean "match characters that are not spaces or quotes". You can be more literal and match \#\{[\w.]+\}.

If you want to keep the value but remove the attribute and closing quote, the easiest approach is to capture the value:

xmlAttributeReplace="([^" ]+)"

and replace with $1.

Working example: https://regex101.com/r/tEOJwh/2

Note that you don't require lookbehind here, nor a regex guru.
Possible caveats, of course, is that you are matching XML with a regex - XML can be complicated, and contain comments, newlines, single quotes, escaped characters, etc.