Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. can someone tell me whats wrong with this expression?

can someone tell me whats wrong with this expression?

Scheduled Pinned Locked Moved Regular Expressions
regexcsharpcomxmlhelp
3 Posts 3 Posters 14 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChekGuy
    wrote on last edited by
    #1

    pretty new to regex. this is a great article. can someone tell me whats wrong with my expression? I'm using this from C#. I'm getting the response from a blog in a malformed xml format. Need to extract entries out of it. In a simple format inpu is similar to the following. string input = @"<entry><id>tag:myblog.com try</entry><entry><id>tag:myblog.com tryagain</entry><entry><id>tag:myblog.com hello </enty>"; I need to identify the number of entries, and then processing each of them. Regex blogsRegEx = new Regex(@"<entry><id>tag:myblog.*</entry>"); MatchCollection blogEntries = blogsRegEx.Matches(input); I always get just 1 entry. It matches the whole thing instead of matching multiple strings in the pattern <entry<id>tag:myblog....</entry>. Can someone help what am I missing here? do i need to use subexpressions here?

    E P 2 Replies Last reply
    0
    • C ChekGuy

      pretty new to regex. this is a great article. can someone tell me whats wrong with my expression? I'm using this from C#. I'm getting the response from a blog in a malformed xml format. Need to extract entries out of it. In a simple format inpu is similar to the following. string input = @"<entry><id>tag:myblog.com try</entry><entry><id>tag:myblog.com tryagain</entry><entry><id>tag:myblog.com hello </enty>"; I need to identify the number of entries, and then processing each of them. Regex blogsRegEx = new Regex(@"<entry><id>tag:myblog.*</entry>"); MatchCollection blogEntries = blogsRegEx.Matches(input); I always get just 1 entry. It matches the whole thing instead of matching multiple strings in the pattern <entry<id>tag:myblog....</entry>. Can someone help what am I missing here? do i need to use subexpressions here?

      E Offline
      E Offline
      egenis
      wrote on last edited by
      #2

      Not sure about the regex (It makes my brain hurt), but you can use linq instead

      string findText = @"tag:myblog";
      int entriesCount = blogsText.Count(t => t.equals(findText)); // you can use Contains() as well I suppose

      Have not tested it but it should give you the correct results by just tweaking your findText variable.

      1 Reply Last reply
      0
      • C ChekGuy

        pretty new to regex. this is a great article. can someone tell me whats wrong with my expression? I'm using this from C#. I'm getting the response from a blog in a malformed xml format. Need to extract entries out of it. In a simple format inpu is similar to the following. string input = @"<entry><id>tag:myblog.com try</entry><entry><id>tag:myblog.com tryagain</entry><entry><id>tag:myblog.com hello </enty>"; I need to identify the number of entries, and then processing each of them. Regex blogsRegEx = new Regex(@"<entry><id>tag:myblog.*</entry>"); MatchCollection blogEntries = blogsRegEx.Matches(input); I always get just 1 entry. It matches the whole thing instead of matching multiple strings in the pattern <entry<id>tag:myblog....</entry>. Can someone help what am I missing here? do i need to use subexpressions here?

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        By default, regex matching is greedy. That is, wildcards will match the longest possible chunk of input, so you need to make your .* non-greedy. You do that by putting a ? after it, so your line becomes

        Regex blogsRegEx = new Regex(@"<entry><id>tag:myblog.*?</entry>");

        If you are going to do ANYTHING nontrivial with regexes, get a copy of Expresso. (See our Free Tools forum for details.) Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups