Solved

RegExp find tags

Posted on 2006-11-02
4
236 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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