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
Solved

RegExp find tags

Posted on 2006-11-02
4
235 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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