Solved

RegExp find tags

Posted on 2006-11-02
4
233 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
  • 3
4 Comments
 
LVL 62

Expert Comment

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

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

Fernando
0
 
LVL 62

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 62

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now