Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex match but do not capture with a ".*"

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading