Solved

RegExp find tags

Posted on 2006-11-02
4
237 Views
Last Modified: 2010-04-23
I have a long  string that has tags i.e. <row> <cell> etc.

I would like to use a regexp to find all the occurences

I.E.

Dim re As New Regex("<.+>")
        Dim mc As MatchCollection = re.Matches(rtbFormattedText.Text)
        Dim m As Match
        For Each m In mc
            Console.WriteLine(m.ToString)
        Next

the problem is it returns the following line as a single match
<f name="Nobel-Regular"><z newsize="24.0"><color name="LexusGold">

How do I return each <*> using regexp?

The point I am trying to acheive is
The text is in a richtext box, when the text changes in the box, I want to go through and highlight all the tags.  Anything in <>.  I was going to use a regexp then find the position start of the match in the RTB and the length, then highlight it.  If anyone can think of a better way, I will except that as well.

Thanks
0
Comment
Question by:wildrobo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17859324
Try it this way.

    Dim re As New Regex("<.+?>")

Fernando
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17859488
Hi wildrobo;

When you use this pattern “<.+>” to find all the occurrences such as <row> <cell> etc. in a string this is what happens:
1.      Regex looks for the first character to match “<”
2.      Then it matches 1 or more characters of any type. So it will read until the end of the current line.
3.      Then it will backtrack until it finds the character “>”
4.      Then return the results
5.      In this case “<f name="Nobel-Regular"><z newsize="24.0"><color name="LexusGold">”

The above pattern is called greedy match because it consumes as many characters as possible.

Using this pattern, “<.+?>” , tells Regex to consume the least number of characters for the match. So you will receive the following matches for the above input.

Match 1 =  “<f name="Nobel-Regular">”
Match 2 =  “<z newsize="24.0">”
Match 3 =  “<color name="LexusGold">”

Fernando
0
 

Author Comment

by:wildrobo
ID: 17859828
Awesome,
   Thank you.  Not only for the actual code, but more importantly for the explination.  "Teach a man to fish...." right.


Thanks again.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17860846
Not a problem, always glad to help if I can. ;=)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question