Home > Net >  Regex match but do not capture with a ".*"
Regex match but do not capture with a ".*"

Time:02-10

I have two blocks of code.

<TabItem Header="{materialDesign:PackIcon Kind=Bank, Size=24}"
                     Style="{StaticResource MaterialDesignNavigationRailTabItem}"
                     ToolTip="Example">

and

<Button Header="{materialDesign:PackIcon Kind=Bank, Size=24}"
                     Style="{StaticResource MaterialDesignNavigationRailTabItem}"
                     ToolTip="Example">

I to select the ToolTip="Example" part and replace it. However, I only want to select the ToolTip that is inside the TabItem block. I can select it with:

ToolTip\=\"(.*?)\"

That selects the one in the Button block as well. I used this StackOverflow Question to try and solve my issue, but I could not figure out how to make it work with a ".*".

So my criteria is:

  1. Must begin with '<TabItem'
  2. Must contain 'ToolTip=".*"'
  3. Must end with ">"

Is what I am trying to achieve possible? If so, how could I achieve this?

CodePudding user response:

This should work

<TabItem\b[^>]*\bToolTip="([^"]*)"[^>]*>

Using [^>]* ensures that the regexp won't match across multiple tags, and [^"]* won't allow that capture group to go outside the quoted tooltip attribute.

You can't use a lookaround for this, because you'd need a lookbehind to match the part before ToolTip, and lookbehinds have to be fixed length in most regexp engines.

If you're using this in a regexp replacement, put the parts that should be kept in the replacement into capture groups, and then use back-references in the replacement string.

CodePudding user response:

<TabItem.*?ToolTip="(.*?)">

if you use this regex then you will be able to get the value inside the tooltip as a list you can use them in regex with $1 here $1 will be the value inside the first tooltip and $2 will have the second if there is a second match for this regex

  • Related