?
Solved

RegExp find tags

Posted on 2006-11-02
4
Medium Priority
?
238 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 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