-2

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?

shinyshark
  • 81
  • 8
  • 3
    It would be better to use a DOM parsing library than regular expression to process HTML. – Barmar Feb 09 '22 at 15:38
  • 2
    You can't parse HTML with regular expressions. There's no notion of "inside" or elements in regular expressions. The conditions you described aren't enough to detect an element - what if there are a lot of `TabItem`s? You'd have to match the end tag. But what if `TabItem` is an empty element, ie `` ? What if one of the `TabItem`s hase no tooltip? The regex would keep searching until it found a `Tooltip` no matter where it came from – Panagiotis Kanavos Feb 09 '22 at 15:39
  • Why do you need to not capture part of it? If you're doing a replacement, just copy that part into the replacement. – Barmar Feb 09 '22 at 15:40
  • @Barmar I want to replace the ToolTip on 100+ instances. But I only want to replace them within the TabItem block. I can do it manually, but I wanted to try and find a regex solution to solve the issue. – shinyshark Feb 09 '22 at 15:43
  • Let's say you tried `` as well. If you added the end tag, you wouldn't be able to catch empty elements. – Panagiotis Kanavos Feb 09 '22 at 15:44

2 Answers2

2

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0
<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

Pratik Agrawal
  • 405
  • 3
  • 17