can someone tell me whats wrong with this expression?
-
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?
-
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?
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 supposeHave not tested it but it should give you the correct results by just tweaking your findText variable.
-
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?
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 becomesRegex 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.