iterate matches in unnamed groups

given a simple RegEx like so: ^(\d+\D+\d+)(.*) and a string to parse as 15-APR-2012 @ 11:00:00 using a replace of [$1]$2 yields [15-APR-2012] @ 11:00:00 as expected.

My issue is when calling Regex.Matches on this I receive one match at index 0 and the entire string: 15-APR-2012 @ 11:00:00 is shown as the match. But there are really 2 matches. How can I get both matches? I should see index 0 and index 12. The code I'm using is from another SO thread:

// Get a collection of matches.
MatchCollection matches = Regex.Matches(inPutString, regexptrn);
foreach (Match match in matches)
    foreach (Capture capture in match.Captures)
            retVal += String.Format("Index={0}, Value={1}", capture.Index, capture.Value) + Environment.NewLine;

Do I have the wrong idea about what .Matches() really does?

OK. this is a follow up same string, new regex "(..)(...)" replacing with [$1]{$2} results in [10]{-AU}[G-]{201}[2 ]{@ 1}[1:]{45:}38 we expect this.

Matches however gives this:

Index=0, Value=10-AU
Index=5, Value=G-201 
Index=10, Value=2 @ 1 
Index=15, Value=1:45: 

This shows that Matches looks at the entire pattern and NOT its groups. Now how to get the group matches


There are two reasons your current regular expression can never get multiple matches:

  • The ^ at the beginning of your regex is an anchor to the beginning of the string, so you need to remove this otherwise you regex can only match at index 0.
  • The .* at the end of your regex will match the remainder of the string, and you won't find any overlapping matches so you can only match once. Since you are not doing anything with the second capture group anyway, you can probably just drop this from your regex. try using the regex (\d+\D+\d+) and replacing matches with [$1].

Need Your Help

How to send JSON object over request to web service in android


I would like to pass JSON object as request to web service like i mentioned below.

Populate 3 combobox from same table but use filter in all 3 tables

c# sql winforms

i have a windows application having 3 combobox. username, prepared by, authorized by.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.